我是一个Java人,我正在尝试用C ++为一个类做一个任务。我无法声明我创建的类型的变量作为另一个类的属性。这是我的方法
private:
HeatingUnit heatingUnit;
int tempToMaintain;
public:
BangBangControl(int tempToMaintain, bool isOn, int initialTemp){
heatingUnit= new HeatingUnit(isOn, initialTemp);
this -> tempToMaintain = tempToMaintain;
}
我收到此错误
BangBangControl.cpp: In constructor ‘BangBangControl::BangBangControl(int, bool, int)’:
BangBangControl.cpp:15: error: no match for ‘operator=’ in ‘((BangBangControl*)this)->BangBangControl::heatingUnit = (((HeatingUnit*)operator new(8u)), (<anonymous>->HeatingUnit::HeatingUnit(((int)isOn), initialTemp), <anonymous>))’
HeatingUnit.h:6: note: candidates are: HeatingUnit& HeatingUnit::operator=(const HeatingUnit&)
BangBangControl.cpp: In member function ‘int BangBangControl::main()’:
BangBangControl.cpp:37: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Temp to maintain is: ")))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](bBC. BangBangControl::getTemp()))), ((const char*)" Current temp is: ")) << bBC. BangBangControl::update()’
/usr/include/c++/4.2.1/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:131: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:177: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:92: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:184: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:106: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:195: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:208: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:217: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/ostream:229: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.2.1/bits/ostream.tcc:120: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
答案 0 :(得分:4)
HeatingUnit heatingUnit;
应该是:
HeatingUnit *heatingUnit;
^^
我相信你的意图是创建一个HeatingUnit
类型的指针,因为你在构造函数中使用了new
。 new
用于将自由存储上的内存分配给指针变量。
如果创建指针不是你的意图(我不确定,因为你是从没有指针的java迁移),只需使用:
HeatingUnit heatingUnit;
但不要使用`new来在构造函数中分配内存。
答案 1 :(得分:2)
在C ++对象中创建时没有new
,除非您想将它们放在堆上。要将它们放在堆上,您需要使用指针,例如
HeatingUnit* heatingUnit;
但是,您真正想要的是从成员初始化列表中初始化对象:
BangBangControl(int tempToMaintain_, bool isOn, int initialTemp):
heatingUnit(isOn, initialTemp),
tempToMaintain(tempToMaintain_)
{
}
答案 2 :(得分:0)
变量声明应该是,
HeatingUnit *heatingUnit;
new HeatingUnit
返回一个指针HeatingUnit*
,并认为你需要一个指针。
对于您的信息,这是另一种方式(有其局限性,但您感觉有点像Java):
private:
HeatingUnit &heatingUnit; // <--- declare reference
int tempToMaintain;
public:
BangBangControl(int tempToMaintain, bool isOn, int initialTemp) :
heatingUnit(*new HeatingUnit(isOn, initialTemp))
{
this -> tempToMaintain = tempToMaintain;
}
请注意,我们在构造函数的初始化列表中定义heatingUnit
。您也可以为tempMaintain
执行此操作。
现在heatingUnit
可以与.
运算符一起使用,例如Java而不是->
。
除了所有这些之外,在C ++中,只有在想要动态执行时才应使用new
。在您的特定情况下,HeatingUnit heatingUnit;
就足够了,它声明了一个自动对象,并且不需要手动重新分配。
答案 3 :(得分:0)
如果你想这样做:
heatingUnit= new HeatingUnit(isOn, initialTemp);
你必须改变
HeatingUnit heatingUnit
到
HeatingUnit *heatingUnit
为什么呢?因为在Java中,所有对象默认只能将引用保存到对象,并且要在C ++中执行此操作,您必须将其声明为指针,而在C ++对象中可以将子对象作为字段(可以不能用Java完成。
此外,在Java中,初始化是在构造函数的主体中完成的,在C ++中使用初始化列表是很好的做法。
BangBangControl(int tempToMaintain, bool isOn, int initialTemp)
: heatingUnit(new HeatingUnit(isOn, initialTemp)),
tempToMaintain(tempToMaintain)
{ }
但是,使用new
要求您必须使用delete
自行清理内存,因此以下内容可能更为可取:
BangBangControl(int tempToMaintain, bool isOn, int initialTemp)
: heatingUnit(isOn, initialTemp),
tempToMaintain(tempToMaintain)
{ }