2013-10-07 20 views
0

我收到以下錯誤任何領域:錯誤:類「軟件」沒有名爲「PTR」

softwarew.hh: In constructor ‘Software::Software(std::string, int)’: 
softwarew.hh:26:45: error: class ‘Software’ does not have any field named ‘ptr’ 
softwarew.hh:28:7: error: ‘ptr’ was not declared in this scope 
softwarew.hh: In destructor ‘Software::~Software()’: 
softwarew.hh:40:6: error: ‘ptr’ was not declared in this scope 

有人能解釋我爲什麼會收到這些錯誤?

會導致該錯誤代碼:

Software(std::string name, int revision) : ptr(software_construct(name.c_str(), revision)) { 

    if(!ptr) throw std::runtime_error("no software created"); 
} 

~Software(){ 
    if(ptr) 
     software_destruct(ptr); 
} 

private: 
struct Software_s* ptr; 
+2

什麼是'Software_s'? –

+3

這些是* only *錯誤還是警告?你能發佈一個完整的,最小的,可編輯的例子嗎? –

+1

簡單 - 它發生是因爲「軟件類沒有任何名爲'ptr'的字段」 –

回答

0

你的錯誤說

"class Software does not have any field named ptr"

鑑於Software_s一些合適的定義,software_constructsoftware_destruct,確保你把該領域的一流作品中:

#ifndef SOFTWAREw_INCLUDED 
#defINE SOFTWAREw_INCLUDED 

class Software{ 
    Software(std::string name, int revision) 
    : ptr(software_construct(name.c_str(), revision)) { 
    if(!ptr) 
     throw std::runtime_error("no software created"); 
    } 

    ~Software(){ 
    if(ptr) 
     software_destruct(ptr); 
    } 

private: 
    struct Software_s* ptr; 
}; 

#endif 
+0

我有完全相同的代碼佈局,但它仍然給我我有錯誤。我通過將聲明和實現放入不同的文件來解決問題。 – Spacy

+0

@Spacy很高興你解決了它,但我不知道在這種情況下究竟發生了什麼。 – doctorlove