我收到以下错误:
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;
答案 0 :(得分:0)
您的错误说
“class Software没有任何名为ptr的字段”
鉴于Software_s
,software_construct
和software_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