【问题标题】:C++ new operator. Creating a new instanceC++ 新运算符。创建新实例
【发布时间】:2009-11-15 03:31:24
【问题描述】:

我在用 C++ 创建对象时遇到了一些问题。我创建了一个名为 Instruction 的类,并尝试创建一个新实例,但出现编译器错误。

类代码:

class Instruction{

  protected:
    string name;
    int value;

  public:
    Instruction(string _name, int _value);
    ~Instruction();
    void setName(string _name);
    void setValue(int _value);
    string getName();
    int getValue();
    virtual void execute();
};



//constructor
inline Instruction::Instruction(string _name, int _value){
    name = _name;
    value = _value;
}
//destructor
inline Instruction::~Instruction(){
    //name = "";
    //value = 0;
}
inline void Instruction::setName(string _name){
     name = _name;
}

inline void Instruction::setValue(int _value){
    value = _value;
}

inline string Instruction::getName(){
       return name;
}

int Instruction::getValue(){
    return value;
}
inline void Instruction::execute(){
    cout << "still have to implement";
}

这就是我尝试创建新对象的方式:

Instruction* inst;
inst = new Instruction("instruction33", 33);

我收到以下编译器错误:

functions.h:70: error: no matching function for call to ‘operator new(unsigned int, std::string&, int&)’
/usr/include/c++/4.3/new:95: note: candidates are: void* operator new(size_t)
/usr/include/c++/4.3/new:99: note:                 void* operator new(size_t, const std::nothrow_t&)
/usr/include/c++/4.3/new:105: note:                 void* operator new(size_t, void*)

你们是对的。错误来自这行代码:

instList.push_back(inst);

instList 的创建位置如下:

list <Instruction> instList;  //#include <list> is in the file

【问题讨论】:

  • 我在代码中添加了问题所在。当我尝试将指令添加到指令列表时会发生这种情况。
  • 你用g++编译吗?我尝试用gcc 编译这段代码,我得到了类似的错误。
  • 我把like改成list instList;它现在编译得很好。谢谢
  • 不。您应该将其更改回指令列表。 AND 你不应该首先动态地创建指令。我敢打赌你没有任何你需要的删除。

标签: c++ class new-operator


【解决方案1】:

inst 是指向指令对象的指针,instList 是指令对象的列表。所以当你尝试instList.push_back(inst) 时它不起作用(它需要一个真实的对象而不是指向它的指针)。你应该改为instList.push_back(*inst)

【讨论】:

    【解决方案2】:

    我认为你最好不要动态创建指令。

    list <Instruction> instList;
    
    instList.push_back(Instruction("instruction33", 33));
    

    请注意,无需使用 new。
    如果你使用 new 你应该删除指针。
    这会增加您尚未准备好的复杂性。

    【讨论】:

    • @Martin:这不也取决于范围吗?例如,如果这是在函数中完成的,即使将 instList 传递回主程序,单个指令对象也将不再存在,对吗?因为它超出了函数的范围。 @unknown:但是是的,如果仅在声明它的范围内使用它,您应该执行上述操作。其他明智的使用 new 是可以的。
    • @aip: 对不起,不。标准容器实际上会复制它们传入的对象。因此,只要列表中的对象存在,列表中的对象就会存在。
    • @Martin:啊,是的。有道理。
    【解决方案3】:

    实际上,您的错误消息似乎与您粘贴在 OP 中的代码没有任何关系。我有一个很好的回应,准备不将 const char * 作为 std::string& 参数传递,但这看起来不是你的问题。您发布的内容不足以查明问题。

    【讨论】:

      【解决方案4】:

      您粘贴的代码没有任何问题,消息中的错误是要检查第 70 行的 functions.h。

      【讨论】:

        【解决方案5】:

        代替:

        instList.push_back(inst);
        

        试试这个:

        instList.push_back(*inst);
        

        您正在尝试将指向指令的指针放入指令列表中。

        【讨论】:

        • 如果您无论如何都存储实例的副本,那么动态分配原始实例是没有意义的。这只是浪费时间(动态分配并不便宜),您必须记住立即删除实例。
        猜你喜欢
        • 2011-08-18
        • 2016-12-12
        • 2022-01-23
        • 2019-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-11
        • 1970-01-01
        相关资源
        最近更新 更多