简体   繁体   English

C ++新运算符。 创建一个新实例

[英]C++ new operator. Creating a new instance

I'm having some trouble creating an object in C++. 我在用C ++创建对象时遇到了一些麻烦。 I create a class called Instruction, and I am trying to create a new instance, but I get compiler errors. 我创建了一个名为Instruction的类,并且尝试创建一个新实例,但是遇到编译器错误。

Class code: 班级代码:

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";
}

This is how I try to create a new object: 这就是我尝试创建新对象的方式:

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

I get the following compiler errors: 我收到以下编译器错误:

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*)

You guys are correct. 你们是正确的。 The error comes from this line of code: 错误来自以下代码行:

instList.push_back(inst);

where instList is created like this: instList是这样创建的:

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

inst is a pointer to an Instruction object and instList is a list of Instruction objects. inst是指向指令对象的指针, instList是指令对象的列表。 So when you try instList.push_back(inst) it doesn't work (it expects a real object not the pointer to it). 因此,当您尝试使用instList.push_back(inst)它将不起作用(它期望一个真实的对象而不是指向它的指针)。 You should instead have instList.push_back(*inst) . 您应该改为拥有instList.push_back(*inst)

I think you would be better of not dynamically creating the Instruction. 我认为您最好不要动态创建指令。

list <Instruction> instList;

instList.push_back(Instruction("instruction33", 33));

Notice there is no need to use new. 请注意,无需使用new。
If you use new you should be deleting the pointer. 如果使用new,则应删除指针。
That adds a whole level of complexity that you are not ready for. 这增加了您还没有准备好的整体复杂性。

actually, it looks like your error message doesn't have anything to do with the code you pasted in your OP. 实际上,您的错误消息似乎与您粘贴到OP中的代码没有任何关系。 I had a very good response ready to go about not passing const char * as a std::string& parameter, but that doesn't look like it's your issue. 我有一个很好的响应,准备不将const char *作为std :: string&参数传递,但这似乎不是您的问题。 What you have posted isn't enough to pinpoint the problem. 您发布的内容不足以找出问题所在。

您粘贴的代码没有任何问题,消息中的错误表明在70行检查了functions.h。

Instead of: 代替:

instList.push_back(inst);

try this: 尝试这个:

instList.push_back(*inst);

You're trying to put a pointer to an Instruction into a list of Instructions. 您试图将一个指令的指针放入指令列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM