Separating Interface from Implementation

Pages: 12
Hey guys , I wanted to ask what am I doing wrong here

first I tried to separate the interface here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  //Gradebook.h
#include<iostream>
#include<string>
using namespace std;

class Gradebook{

public:
	Gradebook(string);
	void setone(string);
	string getone();
	void msg();
private:
	string name;
};


and then the implements here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include<iostream>
#include<string>
#include"Gradebook.h"
using namespace std;

Gradebook::Gradebook(string x){
	setone(x);
}
void Gradebook::setone(string x){
	name = x;
}
string Gradebook::getone(){
	return name;
}
void msg(){
	cout << "welcome " << getone();
}


and then use it in another project it gives me this error :

undefined reference to `Gradebook::Gradebook(std::string)'

what's wrong ?

thanks for your help in advance.
compiles for me. You've missed Gradebook:: on line 15 in your cpp file, but i dont think that's your issue.

header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Gradebook.h
#ifndef GRADEBOOK_H
#define GRADEBOOK_H 

#include<string>


class Gradebook
{
public:
	Gradebook(std::string);
	void setone(std::string);
	std::string getone();
	void msg();

private:
	std::string name;
};

#endif 


cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<string>
#include"Gradebook.h"


Gradebook::Gradebook(std::string x){
	setone(x);
}
void Gradebook::setone(std::string x){
	name = x;
}
std::string Gradebook::getone(){
	return name;
}
void Gradebook::msg(){
	std::cout << "welcome " << getone();
}


test driver:
1
2
3
4
5
6
#include"Gradebook.h"

int main()
{
	Gradebook myBook("WibbleTits");
}

Last edited on
Yeah I forgot to add gradebook in line 15 , but still it doesn't work :(

Did you have your compiler produce object files for both Gradebook.cpp and your test driver, then have the linker link them together? It sounds like you aren't linking the main driver since it isn't finding the constructor.
again, it works for me.

don't do this:
 
using namespace std;


instead prepend all your string calls (and your cout call) with 'std::'.


are you putting this code in different files? i.e. interface in a .h and impl in a .cpp?

Doesnt it make more sense to have the constructor variable modify name directly instead of calling the set function?

what does this have to do with OP's original problem?
Last edited on
Cody0023 wrote:
Doesnt it make more sense to have the constructor variable modify name directly instead of calling the set function?
It is good practice in Java, and only slightly inefficient/harder for the compiler to optimize in C++.
@Zhuge what I did is as i said in the first post and then tried to use them on another project but it gives me the error.

@ mutexe i don't think using " using namespace std; " or std:: on every call is going to matter in this case and yeah I put the files in the same project that I tried to test them on.

@ Cody0023 I'm only testing things ^^
Please explain to us exactly how you are compiling this.
First created the header " in the first post " then created the implements " Gradebook.cpp"

then pulled them to the project that I wanted to test things on " the project folder " and then compiled.
What IDE are you using? You can't just add files to the project folder, you have to tell the IDE about them too.
I used both dev c++ also visual studio c++

also isn't that why I include the header ?
Last edited on
in VS, create a blank empty project.
then add 3 blank files - 2 .cpp's and 1 .h.

then copy my example code from above into each of those files and build.
what happens?
it compiles but it closes fast even though I added getchar(); getchar();

also isn't that why I include the header ?


No. All the header does is include its contents in the source code. Anything that's defined in the header file, will also be defined in the source code that includes that header file.

It doesn't magically cause the source file to be linked against any other object file. You have to actually tell your IDE/compiler to link against other object files you need.
Cutefriendzoned wrote:
it compiles but it closes fast even though I added getchar(); getchar();
http://www.cplusplus.com/forum/beginner/1988/
No man , it's not about that I don't know how to fix this " console command closes fast " :(

if I did this on vs it closes fast and opens a weird window , but when I do it on dev c++ it gives me this error

undefined reference to `Gradebook::Gradebook(std::string)'
Cutefriendzoned wrote:
if I did this on vs it closes fast and opens a weird window
Do you mean, your program is crashing? Now would be a good time to learn how to use Visual Studio's powerful debugger ;)
I uploaded a picture for you guys

http://oi62.tinypic.com/339oc3l.jpg
Pages: 12