PDA

View Full Version : [SOLVED] C++ header files what am I doing wrong



Tpolich
December 15th, 2008, 08:45 PM
I am going to post files if you guys could point out what I am doing wrong it would be great.

file "main.cpp"


#include <iostream>
#include "stuff.h"



int main(){

Cube gg;
gg.show();

}


file "stuff.h"


#ifndef stuff_h
#define stuff_h


class Cube{

public:
void show();


};

#endif


file "stuff.cpp"


#include <iostream>
#include "stuff.h"

void Cube::show(){

std::cout<< "come on work";
};


When I do

g++ main.cpp -o test

yields


/tmp/ccPAbXk8.o: In function `main':
main.cpp:(.text+0x194): undefined reference to `Cube::show()'
collect2: ld returned 1 exit status



What am I doing wrong. Thanks for the help in advance.

Max Carey
December 15th, 2008, 08:59 PM
You have an extraneous semicolon in stuff.cpp at the end of the Cube::show definition. It should look like


#include <iostream>
#include "stuff.h"

void Cube::show(){

std::cout<< "come on work";
}

Sinkingships7
December 15th, 2008, 09:04 PM
Move the Cube::show() function from stuff.cpp into stuff.h and get rid of the semicolon at the end of it.

Is there any particular reason that you want to have a seperate stuff.cpp and stuff.h?

Tpolich
December 15th, 2008, 09:04 PM
You have an extraneous semicolon in stuff.cpp at the end of the Cube::show definition. It should look like


#include <iostream>
#include "stuff.h"

void Cube::show(){

std::cout<< "come on work";
}

I always thought extra semicolons didn't mean anything in c++. I tried it without the semicolon, same error.

Tpolich
December 15th, 2008, 09:07 PM
Move the Cube::show() function from stuff.cpp into stuff.h and get rid of the semicolon at the end of it.

Is there any particular reason that you want to have a seperate stuff.cpp and stuff.h?

I am trying to figure out how to properly use header files in c++. If you have any idea where I could find a guide or if you could explain it I would appreciate it a lot.

mali2297
December 15th, 2008, 09:12 PM
Try compiling with


g++ main.cpp stuff.cpp -o test

ubuser_7
December 15th, 2008, 09:16 PM
You have to compile the class source file too, so it would be something like:



g++ stuff.cpp main.cpp -o test

Tpolich
December 15th, 2008, 09:20 PM
Try compiling with


g++ main.cpp stuff.cpp -o test


It worked!!! thank you, now I have one more favor to ask "why did that work".

Sinkingships7
December 15th, 2008, 09:24 PM
It worked!!! thank you, now I have one more favor to ask "why did that work".

stuff.cpp is another source file, just like main.cpp. They way you were compiling it was only saying to compile main.cpp.

However, there's no reason to have a separate file called stuff.cpp with the code to the class coded in stuff.h.

stuff.h should have everything pertaining to the class Cube, and would also be better named "Cube.h".

Max Carey
December 15th, 2008, 09:24 PM
You were never compiling stuff.cpp so your main method could not resolve Cube::show().

mdurham
December 15th, 2008, 09:27 PM
Move the Cube::show() function from stuff.cpp into stuff.h
This is not good advice, it's better to keep code and definitions separate. It would work for this small example but would cause problems with a larger program.
Cheers, Mike

mkrahmeh
December 15th, 2008, 09:42 PM
separating interface from implementation is a good practice..
furthermore, IMHO proxy classes are adorable, though many fella's think they're obsolete

monkeyking
December 16th, 2008, 05:38 AM
separating interface from implementation is a good practice..
furthermore, IMHO proxy classes are adorable, though many fella's think they're obsolete

What excatly is a proxy class?