C++ Class help

#include <iostream>
using namespace std;

class A{};

int main()
{
A Aobj;
Aobj.hae();
return 0;
}

class A{

public:
int hae() {
cout << "haha" << endl;
}

};

If i put the class above the main function it will have no problem, and i believe that the problem come from my prototype.


error: 'class A' has no member named 'hae'|
error: redefinition of 'class A'|
error: previous definition of 'class A'|

pleas help..thx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A;//prototype

int main(void)
{
A huh;
huh.hae();

return 0;
}

class A
{
void hae()
{
std::cout << "hi\n";
}
};


You declare the prototype of A without {}, only write "class A;".
You can't forward declare classes like that. I think you can omit the braces but it could still cause problems. You'd still have to fully define it in order to instantiate an object of that type.

Is it a problem to have it above main?

If you really don't want it there, why not create a separate header file for it?
Last edited on
ya, even i remove the curly bracket it wont work..
Yeah, I think it's only used that way for referencing it in function prototypes and the like.

If you actually want an instance of it (such as your code above) then you need the class to be fully defined, be it before main or in an included file.
Topic archived. No new replies allowed.