Click to See Complete Forum and Search --> : What is wrong with this piece of C++ code??? (beginner's question)


digitalzero
01-24-2002, 11:27 AM
Hi, I just started learning C++, coming from a Java background.

Basically I want to make a class called Person which just holds an int variable named age and a function getAge() which returns the Person's age.

This is what I've written so far:

Person.h

class Person
{
public:
Person();
~Person();
int getAge();
int age;
};


Person.cpp

#include <iostream.h>
#include <stdlib.h>
#include <string>
#include "Person.h"

Person::Person(): age(0)
{
}

Person::~Person()
{
}

int Person::getAge()
{
return age;
}


and finally the Test class:

#include <iostream.h>
#include <stdlib.h>
#include "Person.h"

int main()
{
Person malcolm;
std::cout << malcolm.getAge() << std::endl;
system("PAUSE");
return 0;
}


I get this error message:
\Temp\ccwFaaaa.o(.text+0x29):test.cpp: undefined reference to `Person::Person(void)'
\Temp\ccwFaaaa.o(.text+0x40):test.cpp: undefined reference to `Person::~Person(void)'
\Temp\ccwFaaaa.o(.text+0x62):test.cpp: undefined reference to `Person::getAge(void)'
\Temp\ccwFaaaa.o(.text+0xac):test.cpp: undefined reference to `Person::~Person(void)'

I'm using DevC++ on Windows.

kmj
01-24-2002, 12:58 PM
it looks like your development environment isn't linking your object files together for some reason. I've never used DevC++ before...

digitalzero
01-24-2002, 01:20 PM
Anyone have any suggestions for a Windows ANSI C++ compiler then?

kmj
01-24-2002, 02:20 PM
I use VC++, but you can do a google search for
djgpp (I think) or cygwin.

Is devC++ a gui ide or a command line compiler? Either way, there should be some kind of switch to tell it to link your files together or something...

Without looking at your development environment, I can't give you a whole lot of info.

digitalzero
01-25-2002, 01:04 AM
DevC++ is a GUI IDE... I'll try looking for an option for linking then.
Thanks!

digitalzero
01-27-2002, 01:30 PM
Still no luck... i tried using g++ and the same error occurs, linker problem.

kmj
01-27-2002, 01:42 PM
Originally posted by digitalzero:
<STRONG>Still no luck... i tried using g++ and the same error occurs, linker problem.</STRONG>

what was your command line when you tried to compile it?

digitalzero
01-27-2002, 11:10 PM
Line was just:
g++ Test.cpp

kmj
01-27-2002, 11:36 PM
Originally posted by digitalzero:
<STRONG>Line was just:
g++ Test.cpp</STRONG>

Okay, I think the problem is that it's trying to create an executable from just your single cpp file (Test.cpp), but it obviously needs the code from Person.cpp in order to have a complete executable. I'm not familiar enough with g++ to give you the info off the bat, but you should be able to figure it out using the man pages. What you need to do is compile each source file (*.cpp) into an object file, and link all those files together. It may be as simple as the command line:

g++ Test.cpp Person.cpp

but I'm not sure...

I suggest you try that, and if that doesn't work, use man g++ to get more information on how to compile and link your files.


---------------

Okay. I just copied your code and tried it; the above line works fine, though the program "PAUSE" doesn't exist in my system so I get a runtime error.

I suggest you use the command line option: -o filename, which specifies the name of the executable.

Also, if you're going to use the command line for your development (which is a fine idea in my opinion), you may want to familiarize yourself with the utility 'make', which helps you to organize and automate your compilation, linking, cleaning, etc. It's cool. And of course you should be using vim. :)

sorry, couldn't help myself..


PS. Oh yeah, and I think I once mentioned that I thought g++ was just a script that runs gcc with flags for c++ and someone told me it wasn't; well, according to the manpage, it is. Just wanted to mention that. :)

digitalzero
01-28-2002, 10:04 AM
thanks for the help! got it all to work... yah i'm on my way to learn make

I Love NY
01-28-2002, 10:09 AM
Um, I also noticed that there's no function type declared for your Person() and ~Person() functions. Don't you need a 'void' tacked on the front of those, both for the prototypes and the actual definitions?

kmj
01-28-2002, 11:04 AM
Originally posted by I Love NY:
<STRONG>Um, I also noticed that there's no function type declared for your Person() and ~Person() functions. Don't you need a 'void' tacked on the front of those, both for the prototypes and the actual definitions?</STRONG>

Person() is a constructor and ~Person() is a destructor. These are special functions for a class that are called implicitly when a class is instantiated or goes out of scope; they don't have return values. I think these are the only cases where you don't have to supply a return value (except, i think, for some compilers which may assume an 'int' return value if none is given).

I Love NY
01-28-2002, 09:27 PM
Yeah... I have just been getting in to classes. I went back and read some more of my book last night, and happened to be reading about constructors/destructors, and boy I feel stupid now :p