Problem with Constructors, Mutators, etc.

Hello. I'm a beginner here. I'm having trouble seeing what I'm doing wrong in this problem.

Here's my assignment:

Write a class name StudentRecord with the following specifications –
o Data members:
name, marks[ ], major, term (use appropriate data type)
o Member functions:
o You are allowed to leave the constructor default
o Implement all get/set methods
o double findAverage( ) – returns the average mark of the student
o void displayProfile( ) – displays the profile of a student in following manner
Name: John Smith
Major: Computer Science
Term: Fall 2015
Term average: 90%
o Hint: Use getline function to include space
• Main function:
o Take input from user for name, major and term name
o Take input from the user for 5 courses s/he has enrolled in this term (you must
put a validation checking that the range of input such that 100<=marks>=0)
o Create an object of StudentRecord class
o Set name, major, term name and the marks[]
o Invoke displayProfile() function to display the information of the student
o Special instruction
o You MUST submit following 3 files –
§ StudentProfile.h (already provided)
§ StudentProfile.cpp
§ StudentProfileMain.cpp
o You must not change the class interface (StudentProfile.h)

Here are my three files:

StudentProfile.h

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
#include <iostream>
using namespace std;

class StudentProfile{

	string name;
	double marks[5];
	string major;
	string term;

	public: 

	StudentProfile();
	
	void setName(string s);
	string getName();

	void setMajor(string m);
	string getMajor();

	void setTerm(string t);
	string getTerm();

	void setMarks(double m[5]);

	double findAverage();
	void displayProfile();

};


StudentProfile.cpp

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
#include "StudentProfile.h"
void StudentProfile::setName(string n){
		name = n;
}
void StudentProfile::setMarks(int m[]){
	for(int i = 0; i < 5; i++)
	marks[i] = m[i];
}
void StudentProfile::setMajor(string m){
major = m;
}
void StudentProfile::setTerm(string t){
term = t;
}
string StudentProfile::getName(){
return name;
}
int* StudentProfile::getMarks(){
return marks;
}
string StudentProfile::getMajor(){
return major;
}
string StudentProfile::getTerm(){
return term;
}
double StudentProfile::findAverage()
{
double sum = 0;
for(int i = 0; i < 5; i++)
	sum += marks[i];
	return sum / 5;
}
void StudentProfile::displayProfile()
{
cout<<"Name: "<<name<<endl;
cout<<"Major: "<<major<<endl;
cout<<"Term: "<<term<<endl;
cout<<"Average: "<<findAverage()<<endl;
}


StudentProfileMain.cpp
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
#include <iostream>
#include "StudentProfile.h"
using namespace std;
int main(){
	string name, major, term;
	cout<<"Enter the name: ";
	getline(cin, name);
	cout<<"Enter the major: ";
	getline(cin, major);
	cout<<"Enter the term: ";
	getline(cin, term);
	double marks[5];
	for(int i = 0; i < 5; i++)
		{
		cout<<"Enter the marks for course #"<<i+1<<": ";
		cin>>marks[i];
		while(marks[i] < 0 || marks[i] > 100)
			{
			cout<<"Marks should be within the range 0 - 100. Try again."<<endl;
			cout<<"Enter the marks for course #"<<i+1<<": ";
			cin>>marks[i];
			}
		}
	StudentProfile student;
	student.setName(name);
	student.setMajor(major);
	student.setTerm(term);
	student.setMarks(marks);
	student.displayProfile();
}


I don't know what the problem is. When I run it, I get a ton of messages I can't understand.
Last edited on
What do you mean?
Are the "unnecessary messages" from your program?
Can you post at least a little of the messages so we can see what in the world is going on?
Here's what I see.

$ g++ StudentProfileMain.cpp -o StudentProfileMain
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x1d3): undefined reference to `St udentProfile::StudentProfile()'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x1d3): relocation truncated to fi t: R_X86_64_PC32 against undefined symbol `StudentProfile::StudentProfile()'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x1f3): undefined reference to `St udentProfile::setName(std::string)'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x1f3): relocation truncated to fi t: R_X86_64_PC32 against undefined symbol `StudentProfile::setName(std::string)'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x21f): undefined reference to `St udentProfile::setMajor(std::string)'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x21f): relocation truncated to fi t: R_X86_64_PC32 against undefined symbol `StudentProfile::setMajor(std::string) '
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x24b): undefined reference to `St udentProfile::setTerm(std::string)'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x24b): relocation truncated to fi t: R_X86_64_PC32 against undefined symbol `StudentProfile::setTerm(std::string)'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x267): undefined reference to `St udentProfile::setMarks(double*)'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x267): relocation truncated to fi t: R_X86_64_PC32 against undefined symbol `StudentProfile::setMarks(double*)'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x273): undefined reference to `St udentProfile::displayProfile()'
/tmp/ccC1cV8v.o:StudentProfileMain.cpp:(.text+0x273): relocation truncated to fi t: R_X86_64_PC32 against undefined symbol `StudentProfile::displayProfile()'
collect2: error: ld returned 1 exit status


This is all in Cygwin, btw.
Last edited on
Those are linker errors saying that those functions can't be found (since they are defined in another file).

If you're trying to build the whole program then you need to include both the code files in the g++ command:
g++ StudentProfileMain.cpp StudentProfile.cpp -o StudentProgram


Alternatively you can build them separately and link them together:
g++ -c StudentProfileMain.cpp
g++ -c StudentProfile.cpp
g++ StudentProfileMain.o StudentProfile.o -o StudentProgram

Okay. Thanks for that. I got confused on how to display that.

I fixed a bunch of errors that I had but am confused by this error.

$ g++ StudentProfileMain.cpp StudentProfile.cpp -o StudentProgram
/tmp/ccthSiJe.o:StudentProfileMain.cpp:(.text+0x1d3): undefined reference ntProfile::StudentProfile()'
/tmp/ccthSiJe.o:StudentProfileMain.cpp:(.text+0x1d3): relocation truncatedR_X86_64_PC32 against undefined symbol `StudentProfile::StudentProfile()'
collect2: error: ld returned 1 exit status


I thought this was declared already.
Last edited on
ntProfile::StudentProfile()
That's doesn't look right.
But it doesn't appear in the code you've shown so far, so that's about all I can say.
It is a fact that you have not defined that particular function (the default constructor of the StudentProfile class).
Nevermind. I solved it.

Thanks for the help, though!
Topic archived. No new replies allowed.