学校:继承,显示我的班级工作?
我有两个类叫做“Person”,另一个叫做“Student”,我一直在追逐它,而我却无法让它编译。我从链接错误到现在的所有内容。学校:继承,显示我的班级工作?
Person.h #include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person: no parameters\n";
}
Person(string a, string b, string c, string d, string e, string f)
{
cout << "Person:" << a << endl;
cout << "Person:" << b << endl;
cout << "Person:" << c << endl;
cout << "Person:" << d << endl;
cout << "Person:" << e << endl;
cout << "Person:" << f << endl;
}
~Person();
void Person::set_values (string a, string b, string c, string d, string e, string f)
{
name = a;
address = b;
city = c;
state = d;
zip = e;
phone_number = f;
}
protected:
string phone_number;
string name;
string address;
string city;
string state;
string zip;
};
Student.h
#include <iostream>
#include <string>
#ifndef Person
#define Person
#endif
using namespace std;
class Student: Person
{
private:
string grade;
string course;
string gpa;
public:
Student()
{
cout << "Person: no parameters\n";
}
Student(string g, string h, string i)
{
cout << "Person:" << g << endl;
cout << "Person:" << h << endl;
cout << "Person:" << i << endl;
}
~Student();
void setClass_values (string g, string h, string i)
{
grade=g;
course=h;
gpa=i;
}
};
friendly2.cpp
#include <iostream>
#include <string>
#include "Person.h"
#include "Student.h"
using namespace std;
int main(void)
{
Person person;
Student student;
person.set_values("Fred", "825 Rice Street", "St. Paul", "MN","55421", "651-488-8888");
student.setClass_values("A", "Truck Driving", "4.0");
system("pause");
return 0;
}
Error 12 error C2039: '{ctor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 14 error C2039: '{ctor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 16 error C2039: '{ctor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 37 error C2039: '{ctor}' : is not a member of 'Student' d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 15 1 friendly
Error 3 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 20 1 friendly
Error 5 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 20 1 friendly
Error 7 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 20 1 friendly
Error 9 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 10 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 11 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 13 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 15 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 17 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 25 1 friendly
Error 27 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 35 1 friendly
Error 28 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 35 1 friendly
Error 29 error C2039: '{dtor}' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>' d:\visual studio 2010\projects\friendly\friendly\student.h 35 1 friendly
Error 38 error C2039: '{dtor}' : is not a member of 'Student' d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 15 1 friendly
Error 39 error C2039: '{dtor}' : is not a member of 'Student' d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 15 1 friendly
Error 42 error C2039: 'setClass_values' : is not a member of 'Student' d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 17 1 friendly
Error 1 error C2059: syntax error : '{' d:\visual studio 2010\projects\friendly\friendly\student.h 12 1 friendly
Error 32 error C2065: 'course' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 37 1 friendly
Error 20 error C2065: 'endl' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 27 1 friendly
Error 23 error C2065: 'endl' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 28 1 friendly
Error 26 error C2065: 'endl' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 29 1 friendly
Error 19 error C2065: 'g' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 27 1 friendly
Error 31 error C2065: 'g' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 36 1 friendly
Error 34 error C2065: 'gpa' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 38 1 friendly
Error 30 error C2065: 'grade' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 36 1 friendly
Error 22 error C2065: 'h' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 28 1 friendly
Error 33 error C2065: 'h' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 37 1 friendly
Error 25 error C2065: 'i' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 29 1 friendly
Error 35 error C2065: 'i' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\student.h 38 1 friendly
Error 36 error C2065: 'person' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 14 1 friendly
Error 40 error C2065: 'person' : undeclared identifier d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 16 1 friendly
Error 41 error C2228: left of '.set_values' must have class/struct/union d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 16 1 friendly
Error 8 error C2676: binary '<<' : 'std::ostream' does not define this operator or a conversion to a type acceptable to the predefined operator d:\visual studio 2010\projects\friendly\friendly\student.h 22 1 friendly
Error 18 error C2676: binary '<<' : 'std::ostream' does not define this operator or a conversion to a type acceptable to the predefined operator d:\visual studio 2010\projects\friendly\friendly\student.h 27 1 friendly
Error 21 error C2676: binary '<<' : 'std::ostream' does not define this operator or a conversion to a type acceptable to the predefined operator d:\visual studio 2010\projects\friendly\friendly\student.h 28 1 friendly
Error 24 error C2676: binary '<<' : 'std::ostream' does not define this operator or a conversion to a type acceptable to the predefined operator d:\visual studio 2010\projects\friendly\friendly\student.h 29 1 friendly
Error 43 error C3861: 'system': identifier not found d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 19 1 friendly
44 IntelliSense: expected a declaration d:\visual studio 2010\projects\friendly\friendly\person.h 10 3 friendly
46 IntelliSense: expected an identifier d:\visual studio 2010\projects\friendly\friendly\student.h 12 1 friendly
47 IntelliSense: identifier "person" is undefined d:\visual studio 2010\projects\friendly\friendly\friendly2.cpp 11 9 friendly
45 IntelliSense: the global scope has no "set_values" d:\visual studio 2010\projects\friendly\friendly\person.h 26 16 friendly
我已经尝试在friendly2.cpp中的两个类,但我似乎继续追逐这个周围,请帮助。
回答:
你的代码工作有以下变化:
person.h #pragma once
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person: no parameters\n";
}
Person(string a, string b, string c, string d, string e, string f)
{
cout << "Person:" << a << endl;
cout << "Person:" << b << endl;
cout << "Person:" << c << endl;
cout << "Person:" << d << endl;
cout << "Person:" << e << endl;
cout << "Person:" << f << endl;
}
~Person()
{}
void Person::set_values (string a, string b, string c, string d, string e, string f)
{
name = a;
address = b;
city = c;
state = d;
zip = e;
phone_number = f;
}
protected:
string phone_number;
string name;
string address;
string city;
string state;
string zip;
};
student.h
#pragma once
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
class Student: Person
{
private:
string grade;
string course;
string gpa;
public:
Student()
{
cout << "Person: no parameters\n";
}
Student(string g, string h, string i)
{
cout << "Person:" << g << endl;
cout << "Person:" << h << endl;
cout << "Person:" << i << endl;
}
~Student()
{}
void setClass_values (string g, string h, string i)
{
grade=g;
course=h;
gpa=i;
}
};
您的问题是,你声明的析构函数没有定义它们。如果插入声明,编译器将不会自动生成。 我还在头文件中添加了#pragma一次。如果您更喜欢传统的includeguard,则应该使用大写字母的文件名(例如#ifndef PERSON_H ...代表Person.h)。对于较大的项目,我建议在头文件中使用include guard。
以上是 学校:继承,显示我的班级工作? 的全部内容, 来源链接: utcz.com/qa/258172.html