Use Class before Class Declaration

How do I declare a class after where you want to use it e.g.: in "main"?

1
2
3
4
5
6
7
8
9
10
11
12
13
int _tmain(int argc, _TCHAR* argv[])
{
	test.internalINTReturn(); // !!! Doesn't Work !!!
}

class _test
{
public:
	int internalINTReturn()
	{
		return 0;
	}
} test;
You can't. You need to declare someting before you can use it. Same for variables and functions.
If you want you're main() on top of the file, you can use headerfiles to store the class declartion in.
But then why does this work?

1
2
3
4
5
6
7
8
9
10
11
int testReturn();

int _tmain(int argc, _TCHAR* argv[])
{
	testReturn();
}

int testReturn()
{
	return 0;
}


I thought this might work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
extern class _test test;

int _tmain(int argc, _TCHAR* argv[])
{
	test.internalINTReturn();
}

class _test
{
public:
	int internalINTReturn()
	{
		return 0;
	}
} test;


It compiles fine with out test.internalINTReturn(); but when you add it in it returns a compile error. ???
Last edited on
In your first example you declare a function, then use it, then define it. In your second example you try to use a (member)function before declaring it, wich will result in an error. You can give the defention later:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
{
  public:
    void function();
};

int main()
{
    A myObject;
    myObject.function();
}

void A::function()
{
    cout<<"Hello World!";
}
I guess thats as close as it can get to what I want.
Thanks for your help Scipio. :)
Last edited on
Typically. Class A should be in it's own Header/Source file combination and just #included by your main file :)
Topic archived. No new replies allowed.