Sign in to follow this  

C++ Class Problems

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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....

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this