Sign in to follow this  

C++ class problem

This topic is 4728 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

I have yet another problem :P. The book has givin you a class and you have to use it but i keep getting "unresolved external errors" when i run it. Here is the class. class Cow { private: char name[20]; char * hobby; double weight; public: Cow(); Cow(const char * nm,const char * ho, double wt); Cow(const Cow & c); ~Cow(); Cow operator=(const Cow & c); friend void ShowCow(Cow & n); }; and here is my code #include "stdafx.h" #include "Class Cow.h" #include <iostream> using namespace std; int main() { Cow one("Manny","",225); ShowCow(one); cin.get(); cin.get(); return 0; } void ShowCow(Cow & n) { cout << "\n" << n.name; cout << "\n" << n.hobby; cout << "\n" << n.weight; } thnx guys can some one point out what i am doing wrong?

Share this post


Link to post
Share on other sites
Now I could be wrong, but I think you have to create a class using the "new" operator like so:

Cow *one = new Cow("Manny", "", 225);

Then make sure you delete it after you're done (delete one;). Not sure if that's your problem though...

Share this post


Link to post
Share on other sites
Have you defined Cow(), Cow(const char * nm,const char * ho, double wt), Cow(const Cow & c), ~Cow(), and Cow operator=(const Cow & c)? If you haven't, you'll get the unresolved external symbol error.

The definitions should be in a file called "Class Cow.cpp", given that your header is called "Class Cow.h". If not, you'll need to define them.

Share this post


Link to post
Share on other sites
Thnx i think i get classes now i was a little confused but i made a simply program and i think i have a clearer understanding.
Thnx i would be totally lost without my book and u guys :P

Share this post


Link to post
Share on other sites
Quote:
Original post by Sam Gamgee
Now I could be wrong, but I think you have to create a class using the "new" operator like so:

Cow *one = new Cow("Manny", "", 225);

Then make sure you delete it after you're done (delete one;). Not sure if that's your problem though...


Sam Gamgee: You are wrong. 1) Classes are not created, they are defined in code. Objects, which are instances of classes or structures, are created. 2) The method you show works, but it is not required that you use new and delete. Class instances are exactly like every other variable in that respect: you can either use new/delete, or just declare it directly.
Both of these methods are acceptable:
Cow *one = new Cow("Manny", "", 225);
if(NULL != one)
{
delete one;
one = NULL;
}
Or:
Cow one("Manny", "", 225);
However, the second method is simpler and safer, so it is preferable. The pointer method has its uses, but it does not have to be used all the time.

Share this post


Link to post
Share on other sites
cppgirl: It's to make msvc happy. New projects by default use stdafx.h as the precompiled header. Every cpp file has to include it or the compiler gets confused , or you have to turn off the precompiled header.

Share this post


Link to post
Share on other sites

This topic is 4728 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