why does this have an exception in VC++ 2008 Express:

#include <conio.h>
#include <iostream>
using namespace std;

class A
{
private:
	int a;
	class B
	{
	public:
		void f();
	};
public:
	B b;
	//A(){} // default constructor
	class C
	{
	public:
		int a;
	};
	B g(B);
};
void A::B::f()
{
	cout<< "A::B::f()" "\n";
	// cout<< a; // error
}
A::B A::g(B c)
{
	cout<< "g called" "\n";
	B ret;
	return ret;
}
int main()
{
	// A::B b; // error
	A::C c = {33333};
	cout<< c.a << "\n";
	A a;
	a.g(a.b);
	_getch();
}

Right, this might be really stupid but do you mean like exception as in

try
{
   // code
   //code
   if(expression1 == expression2) throw Exception("Errrror");
  }
   catch (Exception e){
            cout << e.msg;
    }
catch(...) { cout << "Undefined Error"; }

I'm really stupid :P

yes, by "exception" I mean something like that!
but it's an internal compiler exception.

what exactly are you trying to do?

exactly I want to know why VC++ has problem with this code, while other compilers don't.
this code does nothing important. it's an example of having types as members of a class.

If you compile with -Wall you should've seen the warning warning C4700: uninitialized local variable 'a' used and that's because class A doesn't have a constructor. (you commented it out)

If you compile with -Wall you should've seen the warning warning C4700: uninitialized local variable 'a' used and that's because class A doesn't have a constructor.

-Wall is not a switch in VC++, but that warning is thrown on the default warning level, and the "internal exception" I get when running the code also tells me the same thing. a was not initialized. I think this is a case of not reading the provided error messages. It is a bad habit. I end up working harder on end user support because they like to close error dialogs without reading them. ;)

OK,
I just added a word "static" to the program, why doesn't it have any exception?:

#include <conio.h>
#include <iostream>
using namespace std;

class A
{
private:
	int a;
	class B
	{
	public:
		void f();
	};
public:
	B b;
	//A(){} // default constructor
	class C
	{
	public:
		int a;
	};
	B g(B);
};
void A::B::f()
{
	cout<< "A::B::f()" "\n";
	// cout<< a; // error
}
A::B A::g(B c)
{
	cout<< "g called" "\n";
	B ret;
	return ret;
}
int main()
{
	// A::B b; // error
	A::C c = {33333};
	cout<< c.a << "\n";
	static A a;         //-->>> CHANGE <<<---
	a.g(a.b);
	_getch();
}

-Wall is not a switch in VC++

Ah yes I know :) It's just that at work we call the highest warninglevel "-Wall" no matter what compiler. I should have known it wasn't part of the English language (yet) :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.