0

我有两个类,一个叫做“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中的两个类,但我似乎一直在追逐这个,请帮助。

4

1 回答 1

1

您的代码适用于以下更改:

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 once。如果你更喜欢传统的包含保护,你应该使用大写字母的文件名(例如#ifndef PERSON_H ... for Person.h)。对于较大的项目,我建议在每个标题中使用包含防护。

于 2012-08-28T16:39:29.793 回答