Separating function member from the class definition file

Hi
I have this example working like this:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "ThinkingCap.h"
int main(){
	
	ThinkingCap Estudante, Festa;
	Estudante.Slots("Ola", "Tchau");
	Festa.Slots("Huura!", "Buu!");
	Estudante.PressLeft();
	Festa.PressLeft();
	Estudante.PressRight();
	
	return 0;
}


ThinkingCap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
class ThinkingCap{
public:
	void Slots(string NewLeft, string NewRight);
	void PressLeft();
	void PressRight();
private:
	string LeftString;
	string RightString;
};
void ThinkingCap::Slots(string NewLeft, string NewRight){
	LeftString = NewLeft;
	RightString = NewRight;
}
void ThinkingCap::PressLeft(){
	cout << LeftString << endl;
}
void ThinkingCap::PressRight(){
	cout << RightString << endl;
}


Now when i trie removing the functions from the ThinkingCap.h file to ThinkingCap.cpp e get this second versio ...not working.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "ThinkingCap.h"
int main(){
	
	ThinkingCap Estudante, Festa;
	Estudante.Slots("Ola", "Tchau");
	Festa.Slots("Huura!", "Buu!");
	Estudante.PressLeft();
	Festa.PressLeft();
	Estudante.PressRight();
	
	return 0;
}


ThinkingCap.h
1
2
3
4
5
6
7
8
9
10
11
#include <string>
using namespace std;
class ThinkingCap{
public:
	void Slots(string NewLeft, string NewRight);
	void PressLeft();
	void PressRight();
private:
	string LeftString;
	string RightString;
};


ThinkingCap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include "ThinkingCap.h"
using namespace std;
void ThinkingCap::Slots(string NewLeft, string NewRight){
	LeftString = NewLeft;
	RightString = NewRight;
}
void ThinkingCap::PressLeft(){
	cout << LeftString << endl;
}
void ThinkingCap::PressRight(){
	cout << RightString << endl;
}


What am i missing here?

Thanks
An inclusion guard in your header file?

1
2
3
4
#ifndef THINKINGCAP_H
#define THINKINGCAP_H
//...
#endif /* THINKINGCAP_H */ 


Well, actually it compiles and runs on my system. Did you do a clean build?
Last edited on
Make sure you are compiling and linking both cpp files. With GCC it looks like

g++ main.cpp ThinkingCap.cpp

This produces an a.exe or a.out that should work.

Did this help?

[edit] Fixed it to read g++, as this is a C++ program...[/edit]
Last edited on
To make it easier on all of us, post the error message that you are getting rather than just saying it does not work. We're just guessing here so far!
Thanks to all of you!
I had never compiled a program with two cpp filed ... so i was forgetting the second one :)
I was compiling with
g++ -Wall main.cpp -o main
Big mistake ... now solved.
Thanks again
Yes, that is it. For those reading this, the complete command line will be:

g++ -Wall main.cpp ThinkingCap.cpp -o think

(Please don't name your executables "main". Name it something that explains what the program does...)

If you want to move to something a little more advanced, you can write a makefile to compile things for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# The following are just convenience variables commonly used in makefiles.
# CC is the compiler name and any special flags needed to execute the compiler.
# CFLAGS are compile-flags: things like -Wall, -DDEBUG=1, etc.
# LFLAGS are link-flags: things like -lm and -lcurses and the like.
#
CC     = g++
CFLAGS = -Wall
LFLAGS =

# Assuming Unix:
DELFILE = rm -f
# If you are on Windows, use the following instead:
#DELFILE = del /q

# A 'rule' is a target name, a colon, a list of dependencies, and then the
# instructions to compile the target. Each line of instructions must begin
# with a TAB character (not spaces!). A dependency is either a file name or
# another target in the makefile (or both).
#
# The very first rule names the default target. In other words, if you don't
# specify a target name when you run 'make', then the default target is
# built. (You can have more than one target in your makefiles. This makefile
# has three main targets: 'think', 'clean', and 'cleaner'.)
#

think: main.o ThinkingCap.o
	$(CC) main.o ThinkingCap.o $(LFLAGS) -o think

main.o: main.cpp ThinkingCap.hpp
	$(CC) -c main.cpp

ThinkingCap.o: ThinkingCap.cpp ThinkingCap.hpp
	$(CC) -c ThinkingCap.cpp

clean:
	$(DELFILE) *.o

# This also removes the executable file.
# Works for Unix and Windows... (but may complain about one of the files not
# existing to be deleted...)
cleaner: clean
	$(DELFILE) think think.exe

Save the file as Makefile in the same directory as your source (main.cpp, etc).

This is only a very basic Makefile. There are other things you can do with it, but don't worry too much about it. The GNU make program has a lot of nice extensions --I tend to use it over the usual make when I am using makefiles.

Makefiles have some significant problems, but are still pretty widely used. But the best part is, they make compiling your program very easy:

% make
% ./think

Compiles your program and executes it. If you are using GNU make, type

% gmake

instead. To get rid of those left-over object files (if you want to recompile everything), make the 'clean' target instead of the default:

% make clean

Slick, isn't it?
Last edited on
That would be my next question :)
I am familiar with the qmake tool used in QT.
I make something like:
1
2
3
qmake -project
qmake projectname.pro
make

Line 2 generates the MakeFile for me.

Is there something like this for generating the makefile above for my example?

Can i use GNU automake with this?
Last edited on
Topic archived. No new replies allowed.