我有这个项目,我必须在其中创建一个类,并使用getter和setter来打印信息。
到目前为止,这是我的代码:
我的主:
//Main program
#include <iostream>
#include <string>
#include "studentType.h"
using namespace std;
int main()
{
studentType student;
studentType newStudent("Brain", "Johnson", '*', 85, 95, 3.89);
student.print();
cout << "***************" << endl << endl;
newStudent.print();
cout << "***************" << endl << endl;
return 0;
}
我的studentType.h文件:
#include <iostream>
#include <string>
using namespace std;
class studentType{
public:
//member functions - accessor
string getFirstName();
string getLastName();
char getGrade();
int getTestScore();
int getProgScore();
double getGPA();
//member function - mutators
void setFirstName(string);
void setLastName(string);
char setGrade();
void setTestScore(int);
void setProgScore(int);
void setGPA(double);
//print function
void print();
//constructor
studentType(string, string, char, int, int, double);
studentType() {}
private:
string firstName = "";
string lastName = "";
char courseGrade = ' ';
int testScore = 0;
int programmingScore = 0;
double gpa = 0.00;
}; //end of declaration
***还有我更新的studentTypeImp.cpp代码:
#include <iostream>
#include <string>
#include "studentType.h"
using namespace std;
studentType::studentType(string s, string t, char a, int x, int y, double z){
firstName = s;
lastName = t;
courseGrade = a;
testScore = x;
programmingScore = y;
gpa = z;
}
void studentType::setFirstName(string first){
firstName = first;
}
void studentType::setLastName(string last){
lastName = last;
}
string studentType::getFirstName(){
return firstName;
}
string studentType::getLastName(){
return lastName;
}
char studentType::getGrade(){
return courseGrade;
}
void studentType::setTestScore(int y){
testScore = y;
}
int studentType::getTestScore(){
return testScore;
}
void studentType::setProgScore(int a){
programmingScore = a;
}
int studentType::getProgScore(){
return programmingScore;
}
void studentType::setGPA(double n){
gpa = n;
}
double studentType::getGPA(){
return gpa;
}
void studentType::print(){
cout << "Name: " << firstName << " " << lastName << endl;
cout << "Grade: " << courseGrade << endl;
cout << "Test score: " << testScore << endl;
cout << "Programming score: " << programmingScore << endl;
cout << "GPA: " << gpa << endl;
}
运行代码时,我获得了对错误的未定义引用
In function `main':
main.cpp:(.text+0xbf): undefined reference to `studentType::studentType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, int, int, double)'
collect2: error: ld returned 1 exit status
/bin/bash: line 4: ./a.out: No such file or directory
我对编码还很陌生,任何帮助都会很棒!
新错误消息:
Build Status
Build Failed
Build Output
/root/sandbox79bdc12a/nt-test-580dc59a.cpp: In member function 'virtual void setAndGetGrade_1_Test::TestBody()':
/root/sandbox79bdc12a/nt-test-580dc59a.cpp:7:22: error: no matching function for call to 'studentType::setGrade()'
student.setGrade();
^
In file included from /root/sandbox79bdc12a/nt-test-580dc59a.cpp:2:0:
/root/sandbox79bdc12a/studentTypeImp.cpp:32:6: note: candidate: void studentType::setGrade(char)
void studentType::setGrade(char p){
^~~~~~~~~~~
/root/sandbox79bdc12a/studentTypeImp.cpp:32:6: note: candidate expects 1 argument, 0 provided
make[2]: *** [CMakeFiles/runTests.dir/nt-test-580dc59a.cpp.o] Error 1
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
make: *** [all] Error 2