C++ Class Problems
ok... I used to use Java and now I am tring to learn C++... When i try to make a class and call it from my main class i get errors, here is what i am doing...
/*Class*/
class Hello
{
public int getNumb()
{
return 100;
}
}
/*Main*/
int main()
{
Hello hey;
cout << hey.getNumb() << endl;
return 0;
}
//////
thats an example of how i put together the class and calling it from the main class...when i run the program i get an error for the line in the main class where i declared " Hello hey; "
can anyone explain to me how i can do this, the book i have is pretty much worthless to me right now....
class Hello
{
public:
int getNumb()
{
return 100;
}
};
would be the correct syntax (minus nice tabbing).
Edit:
I would recommend "Thinking in C++". Its a free book available online, and its pretty solid.
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
{
public:
int getNumb()
{
return 100;
}
};
would be the correct syntax (minus nice tabbing).
Edit:
I would recommend "Thinking in C++". Its a free book available online, and its pretty solid.
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
alright...I have the class set like that but how do i set up the main class to call to that class.... In the book i have it says just to say something like :
int main()
{
ClassName name;
name.classMethod();
//do something with it
return 0;
}
but this is not working for my program, i get an error that says:
ClassName undeclared
//obviously instead of ClassName I have the name of the actual class, but im just saying this as an example
int main()
{
ClassName name;
name.classMethod();
//do something with it
return 0;
}
but this is not working for my program, i get an error that says:
ClassName undeclared
//obviously instead of ClassName I have the name of the actual class, but im just saying this as an example
Well, is the class declared/defined? Be aware that C++ uses a sort-of-single-pass compilation, which means that unlike in Java, you do not have the benefit of what I will here dub declaration lookahead. What this means is that if you want to use a class in your main function, the class needs to be declared before main. If it resides in another file, presumably a header, that header must be #included—before main.
alright thats what i needed...I guess i had to do an
#include "Class.cpp";
now its working...thanks man
#include "Class.cpp";
now its working...thanks man
Beep! Wrong. You should never, ever #include source files (.c, .cc, .cpp), but only headers. (With the possible exception of template definition inclusion. Don't worry about it right now.) See Kylotan's guide to learn more about how to split things up correctly. It is not the same as in Java.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement