为我在 C++ 中创建的对象声明变量
我是一名 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>]
I am a Java guy and I'm trying to do an assignment with C++ for a class. I am having trouble declaring a variable of a type I created as a property of another class. This is my approach
private:
HeatingUnit heatingUnit;
int tempToMaintain;
public:
BangBangControl(int tempToMaintain, bool isOn, int initialTemp){
heatingUnit= new HeatingUnit(isOn, initialTemp);
this -> tempToMaintain = tempToMaintain;
}
I get this error
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>]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应该是:
我相信您的意图是创建一个
HeatingUnit
类型的指针,因为您在构造函数中使用了new
。new
用于将自由存储上的内存分配给指针变量。如果创建指针不是您的意图(我不确定,因为您是从没有指针的 java 迁移的),只需使用:
但不要使用 `new 在构造函数中分配内存。
should be:
I believe your intention is to create a pointer of the type
HeatingUnit
since you usenew
inside the constructor.new
is used to allocate memory on free store to pointer variables.If creating a pointer is not your intention(I am not sure since you are migrating from java which doesn't have pointers), just use:
but do not use `new to allocate the memory in the constructor.
在 C++ 中,对象是在没有
new
的情况下创建的,除非您想将它们放在堆上。要将它们放在堆上,您需要使用指针,例如但是,您真正想要的是从成员初始值设定项列表中初始化对象:
In C++ object are created without
new
unless you want to put them on the heap. To put them on the heap you'd need to use a pointer, e.g.However, what you actually want is to initialize the object from the member initializer list:
变量声明应该是,
new HeatingUnit
返回一个指针HeatingUnit*
并保存您需要一个指针。供您参考,这里有另一种方法(它有其局限性,但您感觉有点像 Java):
请注意,我们在构造函数的初始值设定项列表中定义
heatingUnit
。您也可以对tempMaintain
执行此操作。现在,
heatingUnit
可以像 Java 一样与.
运算符一起使用,而不是->
。除此之外,在 C++ 中,只有当您想动态执行时才应该使用
new
。在您的具体情况下,HeatingUnit heatUnit;
就足够了,它声明一个自动对象,不需要手动进行任何释放。The variable declaration should be,
new HeatingUnit
returns a pointerHeatingUnit*
and to hold that you need a pointer.For your information, here is another way (which has its limitation, but you feel somewhat like Java):
Note that, we are defining
heatingUnit
in the initializer list of the constructor. You can do this for thetempMaintain
as well.Now
heatingUnit
can be used with.
operator like Java instead of->
.All these apart, in C++ you should use
new
only if you want to do it dynamically. In your specific case,HeatingUnit heatingUnit;
would suffice, which declares an automatic object and doesn't require any deallocation manually.如果你想做:
你必须改为
为什么
?因为在 Java 中,默认情况下所有对象只能保存对对象的引用,而要在 C++ 中做到这一点,您必须将其声明为指针,而在 C++ 中,对象可以将子对象作为字段(可以不能用 Java 完成)。
此外,虽然在 Java 中,初始化是在构造函数主体中完成的,但在 C++ 中使用初始化列表是一个很好的做法。
但是,使用
new
要求您必须使用delete
自行清理内存,因此以下内容可能更可取:If you want to do:
You have to change
to
Why? Because in Java all objects can only hold references to objects by default, and to do that in C++ you have to declare it as a pointer, while in C++ objects can have sub-objects as fields (which can`t be done in Java).
Also, while in Java, initialization is done in the body of the constructor, it is good practice in C++ to use initialization lists.
However, using
new
requires that you have to cleanup the memory yourself usingdelete
so the following would be probably be preferable: