Classes in separate files in Gedit?

I'm having trouble with using a class in a separate file from main.cpp.
I learned from a tutorial using Codeblocks and the instructor created a new class and it had templates set up for the header and source files for the new class.
I'm using Gedit in Ubuntu and I'm not really sure how to build a new class like you can with Codeblocks, so I just typed in the template from the tutorial. I keep getting an error and I was wondering if it was because the template was specific to Codeblocks or something of the sorts? Or, is it just my code that's the problem?

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Apple.h"

using namespace std;

int main(){

	Apple appleObject;
	
	return 0;
}



Apple.h:
1
2
3
4
5
6
7
8
9
10
11
#ifndef APPLE_H
#define APPLE_H

class Apple{

	public:
		Apple(void);

};

#endif 



Apple.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "Apple.h"

using namespace std;

Apple::Apple(void){

	cout << "I am a banana!\n";

}




The error I get is:

/tmp/ccC63aaO.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `Apple::Apple()'
collect2: ld returned 1 exit status




I also tried the same code in Sublime Text 2 with gcc and received the same error.
Last edited on
You aren't compiling Apple.cpp. I'm not sure how you would do it in Gedit, but either you would need to add it to some kind of project, or change the command line to something like the following (assuming GCC):

g++ -c -o main.o main.cpp -Wall -Wextra
g++ -c -o apple.o apple.cpp -Wall -Wextra
g++ -o program main.o apple.o

This is so that the linker can find the constructor that is defined in Apple.cpp.
Last edited on
Oh! I completely forgot about that.
Thank you so much! I feel silly :D


gcc Apple.cpp main.cpp -o test -lstdc++

^ I tried this and it compiled beautifully.
Topic archived. No new replies allowed.