错误:没有在'_______'类中声明的'__________'成员函数

时间:2013-04-28 06:31:34

标签: c++

我认为自己是一个相当新手的c ++程序员,我之前从未遇到过这个错误。

我只是想为我的函数创建一个类,但我头文件中声明的所有std :: prefixed函数都没有被识别

//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#ifndef PERSON_H
#define PERSON_H

#include <string>

class Person
{
    public:
        Person();
        std::string getName();  //return first name
        std::string getSurname();//return surname
        int getWeight();    //return weight
        int getBirthYear(); //return birthyear


    private:
//self explanatory member variables but need to be accessible to patient
        std::string m_name;
        std::string m_surname;
        int m_weight;
        int m_birthYear;
};

#endif      

的.cpp

//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#include "Person.h"

Person::Person()
{
    m_name = "name";
    m_surname = "surname";
    m_weight = 0;
    m_birthYear = 0;
    return;
}

//returns m_name
std::string Person::getName()   
{
    return m_name;
}

//returns m_surname
std::string Person::getSurname()
{
    return m_surname;
}

//returns persnon's weight
int Person::getWeight()
{
    return m_weight;
}   

//returns the person's birth year
int Person::getBirthYear()
{
    return m_birthYear;
}

//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#include "Person.h"
#include <iostream>

using namespace std;

int main()
{
//  Person matt;
//  cout << matt.getName() << endl;
//  cout << matt.getSurname() << endl;
//  cout << matt.getWeight() << endl;
//  cout << matt.getBirthYear() << endl;
    return 0;
}

这是我收到的错误

g++ Main.cpp Person.h Person.cpp -o test
Person.cpp: In constructor ‘Person::Person()’:
Person.cpp:17:2: error: ‘m_name’ was not declared in this scope
Person.cpp:18:2: error: ‘m_surname’ was not declared in this scope
Person.cpp: At global scope:
Person.cpp:35:29: error: no ‘std::string Person::getName()’ member function declared in class ‘Person’
Person.cpp:41:32: error: no ‘std::string Person::getSurname()’ member function declared in class ‘Person’

任何想法我做错了什么?这个完全相同的std :: formatting之前对我有用,但由于某种原因,现在只有std :: string函数在尝试创建一个简单的Person类时才被识别。

1 个答案:

答案 0 :(得分:9)

来自评论:

g++ Main.cpp Person.h Person.cpp -o test

正如chris指出的那样,在编译命令行中包含头文件是不常见的。您之前可能使用的稍微不同的调用:

g++ -c Main.cpp Person.h Person.cpp

创建Main.oPerson.o,还有Person.h.gch预编译的标头。预编译的头文件不会使用您当前的构建命令重新生成,但仍在使用,因此对Person.h的更改不会被选中。