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