2015-09-12 61 views
0

你好我想完成我的家庭作業。當我嘗試分離課程時出現編譯錯誤,然後再調用它。但整個測試功能正常工作。它在整個文本中都有類。基本上,當我嘗試從文本中分離類時,我有一條錯誤消息。類的頭,串沒有指定類型

#include <iostream> 
#include<string> 
using namespace std; 

class Person 
{ 
private: 
string alpha; 
int beta; 

public: 
Person(string Name, int Age) 
{ 
    alpha = Name; 
    beta = Age; 
} 
string getName() 
{ 
    return alpha; 
} 
int getAge() 
{ 
    if (beta < 0) 
    { beta = 0; 
     cout << "Error. A negative age cannot be entered. " << endl; 
     } 
    if (beta > 120) 
    { 
     cout << "Damn you're old. How many heart transplants have you had? You Vampire " << endl; 
    } 
    return beta; 
} 
void setName(string alpha) 
{ 

} 
void setAge(int beta); 
void display(); 

}; 

int main() 
{ 


Person Lu("Jess ", 22); 
Person Rose("Gary ", 49); 
cout << Lu.getAge() << " " << Lu.getName() <<endl; 
cout << Rose.getAge() << " " << Rose.getName() << endl; 
return 0; 
}` 

但是,當我分開類,:

#include <iostream> 
#include <string> 

class Person 
{ 
private: 
    string alpha; 
    int beta; 

public: 
    Person(string Name, int Age) 
{ 
    alpha = Name; 
    beta = Age; 
} 
string getName() 
{ 
    return alpha; 
} 
int getAge() 
{ 
    if (beta < 0) 
    { beta = 0; 
     cout << "Error. A negative age cannot be entered. " << endl; 
     } 
    if (beta > 120) 
    { 
     cout << "Damn you're old. How many heart transplants have you had? You Vampire " << endl; 
    } 
    return beta; 
} 
void setName(string alpha) 
{ 

} 
void setAge(int beta); 
void display(); 

}; 

主文件

#include <iostream> 
#include "Person.h" 
#include <string> 
using namespace std; 


int main() 
{ 

Person Lu("Jess ", 22); 
cout << Lu.getAge() << " " << Lu.getName() <<endl; 

    return 0; 
}` 

但是,當我分開類,我得到的代碼塊的錯誤。請幫忙。

+0

有什麼錯誤訊息? – approxiblue

+0

第7行錯誤:「串」沒有指定類型 –

+0

您應編輯爲你的問題。 – approxiblue

回答

1

你忘了using namespace std;在Person.h。

而且,你不必對任何Person.h頭警衛,不會在這樣一個簡單的程序產生問題,但會盡快爲多個文件包括Person.h。

+0

非常感謝!!!!! –

+1

@MichaelLa我應該提到,在標頭中使用'namespace std;'是非常危險的。見http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice –