0

我收到以下错误:

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;
4

1 回答 1

0

你的错误说

“类软件没有任何名为 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
于 2013-10-07T12:15:17.843 回答