question about constructors

I am not sure what I am doing wrong but I can't get this constructor working. Can anyone look at it and point out what my mistake is?

Header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>

using namespace std;

class GradeBook
{
public:
	GradeBook(string, string);
	
	void setCourseName(string);
	void setTeacherName(string);
	
	string getCourseName();
	string getTeacherName();
	
	void displayMessage();

private:
	string courseName;
	string teacherName;
	string name;
	string teacher;
};


cpp file:

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
#include <iostream>
#include "GradeBook.h"

using namespace std;

GradeBook::GradeBook(string name, string teacher)
{
	setCourseName(name);
	setTeacherName(teacher);
}

void GradeBook::setCourseName(string name)
{
	courseName = name;
}

void GradeBook::setTeacherName(string teacher)
{
	teacherName = teacher;
}

string GradeBook::getTeacherName()
{
	return teacherName;
}

string GradeBook::getCourseName()
{
	return courseName;
}

void GradeBook::displayMessage()
{
	cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
	cout << "Being taught by" << getTeacherName() << "." << endl;
}
What is it that doesn't work?


1
2
3
4
5
int main()
{
	GradeBook book("English", "Mr. Andrew");
	book.displayMessage();
}
Welcome to the grade book for
English!
Being taught byMr. Andrew.

I guess the output could look nicer but it is working.
Last edited on
Peter,

when I compile the program I get the following error:

1>------ Build started: Project: Chapter_3, Configuration: Debug Win32 ------
1>Compiling...
1>GradeBook.cpp
1>Linking...
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Nadia\Documents\Visual Studio 2008\Projects\Chapter_3\Debug\Chapter_3.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://c:\Users\Nadia\Documents\Visual Studio 2008\Projects\Chapter_3\Debug\BuildLog.htm"
1>Chapter_3 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

So I am not sure why. Your output is what it is suppose to be produced.
Last edited on
I have gone over this constructor and I can't figure out why I am getting the error above.
Looks like it can't find the main function. Did you add a main function? If you use more than one .cpp file, did you link properly?
No, I haven't build the main function yet. I guess that is what is missing. Let me build it and see what happens.
Peter,

I feel like an idiot. The program works properly once I created the cpp file with the main in it.

Thanks!!!
Topic archived. No new replies allowed.