2013-01-12 36 views
0

我不斷收到此錯誤:no matching function for call to 'Person::Person(const char [10]) 該類在一個單獨的cpp文件中。當我在相同的cpp文件中有構造函數時,我可以輕鬆創建一個對象。這是我的代碼:如何用C++中的參數構造一個對象

的main.cpp文件

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

using namespace std; 

int main() 
{ 
    Person p("hellooooo"); 
    return 0; 
} 

Person.h文件

#ifndef PERSON_H 
#define PERSON_H 


class Person 
{ 
    public: 
     Person(); 
    protected: 
    private: 
}; 

#endif // PERSON_H 

Person.cpp文件

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

using namespace std; 

Person::Person() 
{ 
    cout << "this is the default constructor??"; 
} 

Person::Person(string n) 
{ 
    cout << n; 
} 

回答

6

你必須添加第二個構造的聲明在您的.h文件中

#include <string> 
class Person 
{ 
    public: 
     Person(); 
     Person(std::string); 
}; 
+0

感謝和+1。 (另外,我編輯了這個問題......術語,無處不在的術語......:P) – 2013-01-12 21:02:50

+0

+1,或者將構造函數改爲Person(const std :: string&n =「這是構造函數」)並且只是'cout << n;'在身體裏。無論哪種方式工作。 – WhozCraig

相關問題