Click to See Complete Forum and Search --> : simple class in C++
yolabingo
12-03-2002, 11:55 PM
Why won't this compile ?!? I've got 3 files, main.cc, and a poo.cc and poo.h, which define a simple class. G++ tells me I've got "undefined reference to poo::poo(void)"
//main.cc
#include "poo.h"
using namespace std;
int main() {
Poo temp;
return 0;
}
//poo.h
#ifndef POO_H
#define POO_H
class Poo {
public:
Poo();
Poo(int n);
~Poo();
void GetData();
private:
int data;
};
#endif
//poo.cc
#include "poo.h"
#include <cstdlib>
#include <iostream>
Poo::Poo() {
data = 0;
}
Poo::Poo(int n) {
data = n
}
Poo::~Poo() {
data = 0;
}
void Poo::GetData() {
cout << endl << data << endl;
}
Palin
12-04-2002, 12:12 AM
It would help if you said what exactly you typed to compile this so we could see what that was doing as for the code it looks fine. you could try building a makefile would look something like this
main: main.o poo.o
g++ -o main main.o poo.o
main.o: main.cc poo.h
g++ -c main.cc
poo.o: poo.cc poo.h
g++ -c poo.cc
clean:
rm -f main.o poo.o main
then at the command line type make. also make sure you hit tab to indent the g++,rm commmands to make sure that make doesn't give you an error.
yolabingo
12-04-2002, 12:22 AM
I get:
bash-2.05a$ g++ main.cc
/tmp/cc3bvLv8.o: In function 'main':
/tmp/cc3bvLv8.o(.text+0xe): undefined reference to 'Poo::Poo(void)'
/tmp/cc3bvLv8.o(.text+0x1f): undefined reference to 'Poo::~Poo(void)
collect2: ld returned 1 exit status
Palin
12-04-2002, 12:33 AM
prolly would be best if you used a make file the one I listed should work for your program you just need to have make installed on your box.
you could also type g++ main.cc poo.cc
this should produce an output file called a.out
if you want to name the file type
g++ -o <name> main.cc poo.cc
M3_!ha^
12-04-2002, 01:01 AM
Originally posted by yolabingo
I get:
bash-2.05a$ g++ main.cc
/tmp/cc3bvLv8.o: In function 'main':
/tmp/cc3bvLv8.o(.text+0xe): undefined reference to 'Poo::Poo(void)'
/tmp/cc3bvLv8.o(.text+0x1f): undefined reference to 'Poo::~Poo(void)
collect2: ld returned 1 exit status You can't do this because your constructor and destructor are in poo.cc. The compiler has no clue that it needs to also compile poo.cc with main.cc.
You can either #include "poo.cc", or do what Palin suggested and compile the two files together with g++ main.cc poo.cc.
yolabingo
12-04-2002, 01:05 AM
Thanks! It compiles fine now...