C ++分离类文件问题

时间:2015-11-17 20:17:51

标签: c++ visual-studio-2015

我正在开展另一个学校项目,我应该为我的班级使用多个文件......即whatever.hwhatever.cpp。我完全知道如何使用它们,但出于某种原因,当我开始使用它们构建代码时,我会遇到大量错误。我会发布我的Person.hPerson.cpp中的代码,然后是我得到的错误。

Person.h

#pragma once
class Person
{
public:
    Person();
    ~Person();
    void setGender(string Gen);
    string getGender();
private:
    int Age;
    string Gender;
    int AnnualIncome;


};

Person.cpp

#include "Person.h"
#include <iostream>
#include <string>
#include <vector>     //I'll be using this later.
#include <fstream>    //I'll be using this later.

using namespace std;

Person::Person()
{

}


Person::~Person()
{
}

void Person::setGender(string Gen)
{
    Gender = Gen;
}

string Person::getGender()
{

    return string(Gender);
}

我得到的错误如下:

Error   C2061   syntax error: identifier 'string'   Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    7   Build

Error   C2061   syntax error: identifier 'string'   Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    7   Build

Error   C3646   'getGender': unknown override specifier Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    8   Build

Error   C2059   syntax error: '('   Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    8   Build

Error   C2238   unexpected token(s) preceding ';'   Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    8   Build

Error   C3646   'getGender': unknown override specifier Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    8   Build

Error   C2059   syntax error: '('   Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    8   Build

Error   C2238   unexpected token(s) preceding ';'   Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    8   Build

Error   C3646   'Gender': unknown override specifier    Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    11  Build

Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    11  Build

Error   C3646   'Gender': unknown override specifier    Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    11  Build

Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.h    11  Build

Error   C2511   'void Person::setGender(std::string)': overloaded member function not found in 'Person' Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp  19  Build

Error   C2065   'Gender': undeclared identifier Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp  20  Build

Error   C2039   'getGender': is not a member of 'Person'    Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp  23  Build

Error   C2065   'Gender': undeclared identifier Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\person.cpp  26  Build

Error   C2660   'Person::setGender': function does not take 1 arguments Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\source.cpp  32  Build

Error   C2039   'getGender': is not a member of 'Person'    Marketing Software  c:\users\danny\documents\visual studio 2015\projects\marketing software\source.cpp  33  Build

我不知道该怎么办,因为我已经多次尝试重做,我已经查找了那些特定的错误,我无法弄清楚我的程序有什么问题。

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,你应该做两件事:

  1. 尽可能将所需的标题直接包含在您自己的标题文件中,以使其独立运行,而不依赖于包含顺序。除非您string
  2. ,否则未定义#include <string>
  3. 对头文件中的类型使用完全限定名称:全局命名空间中不存在string,因为std::string位于命名空间std中,因此您应该通过它来引用它std::string
  4. 最后,在用户定义的标头之前添加官方库标头通常是个好主意,因此您应该在{cp文件中string之前添加vectorPerson.h

    这些更改意味着您的特定情况,年份标题变为:

    Person.h

    #pragma once
    
    #include <string>
    
    class Person
    {
    public:
        Person();
        ~Person();
        void setGender(std::string Gen);
        std::string getGender();
    private:
        int Age;
        std::string Gender;
        int AnnualIncome;
    };
    

    你的cpp文件变为:

    Person.cpp

    #include <iostream>
    #include <string>
    #include <vector>     //I'll be using this later.
    #include <fstream>    //I'll be using this later.
    #include "Person.h"
    
    using namespace std;
    
    Person::Person()
    {
    
    }
    
    
    Person::~Person()
    {
    }
    
    void Person::setGender(string Gen)
    {
        Gender = Gen;
    }
    
    string Person::getGender()
    {
        return Gender;
    }
    

    最后,一句轻微的评论:使用using namespace std通常不是一个好主意,因为这会导入所有内容,通常最好只选择您需要的内容,例如using std::string;using std::vector;等等。