2013-04-28 190 views
5

我認爲自己是一個相當新手的C++程序員,我從來沒有經歷過這個錯誤。錯誤:沒有在類'_______'中聲明的'__________'成員函數

我只是想創建一個類爲我的功能,但我所有的std ::我的頭文件中聲明前綴的功能不被認可

//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類時無法識別。

+7

你的構建命令中的標題是什麼?人們對構造函數初始化列表有什麼作用? – chris 2013-04-28 06:34:06

+0

不幸的是,這是我被教導編程的唯一途徑,也是我的導師/標記首選的方式,因爲它更容易讓他們脫脂 – 2013-04-28 06:36:26

+8

那麼你的導師對此是錯誤的。要求退款。 – juanchopanza 2013-04-28 06:37:51

回答

9

從評論:

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

克里斯指出,這是不尋常的,包括在編譯命令行的頭文件。稍微不同的調用,你可能之前使用:

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

創建Main.oPerson.o,也Person.h.gch編譯頭。預編譯的頭文件不會使用當前的構建命令重新生成,但仍在使用,因此對Person.h的更改不會被拾取。

相關問題