为我在 C++ 中创建的对象声明变量

发布于 2025-01-03 16:11:33 字数 6034 浏览 0 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丢了幸福的猪 2025-01-10 16:11:33
HeatingUnit heatingUnit;

应该是:

HeatingUnit *heatingUnit;
            ^^

我相信您的意图是创建一个 HeatingUnit 类型的指针,因为您在构造函数中使用了 newnew 用于将自由存储上的内存分配给指针变量。

如果创建指针不是您的意图(我不确定,因为您是从没有指针的 java 迁移的),只需使用:

   HeatingUnit heatingUnit;

但不要使用 `new 在构造函数中分配内存。

HeatingUnit heatingUnit;

should be:

HeatingUnit *heatingUnit;
            ^^

I believe your intention is to create a pointer of the type HeatingUnit since you use new 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:

   HeatingUnit heatingUnit;

but do not use `new to allocate the memory in the constructor.

雪落纷纷 2025-01-10 16:11:33

在 C++ 中,对象是在没有 new 的情况下创建的,除非您想将它们放在堆上。要将它们放在堆上,您需要使用指针,例如

HeatingUnit* heatingUnit;

但是,您真正想要的是从成员初始值设定项列表中初始化对象:

BangBangControl(int tempToMaintain_, bool isOn, int initialTemp):
    heatingUnit(isOn, initialTemp),
    tempToMaintain(tempToMaintain_)
{
}

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.

HeatingUnit* heatingUnit;

However, what you actually want is to initialize the object from the member initializer list:

BangBangControl(int tempToMaintain_, bool isOn, int initialTemp):
    heatingUnit(isOn, initialTemp),
    tempToMaintain(tempToMaintain_)
{
}
偏闹i 2025-01-10 16:11:33

变量声明应该是,

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 heatUnit; 就足够了,它声明一个自动对象,不需要手动进行任何释放。

The variable declaration should be,

HeatingUnit *heatingUnit;

new HeatingUnit returns a pointer HeatingUnit* 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):

  private:
    HeatingUnit &heatingUnit;  // <--- declare reference
    int tempToMaintain;

  public:
    BangBangControl(int tempToMaintain, bool isOn, int initialTemp) :
    heatingUnit(*new HeatingUnit(isOn, initialTemp))
    {
      this -> tempToMaintain = tempToMaintain;
    }

Note that, we are defining heatingUnit in the initializer list of the constructor. You can do this for the tempMaintain 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.

﹉夏雨初晴づ 2025-01-10 16:11:33

如果你想做:

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)
{ }

If you want to do:

heatingUnit= new HeatingUnit(isOn, initialTemp);

You have to change

HeatingUnit heatingUnit

to

HeatingUnit *heatingUnit

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.

BangBangControl(int tempToMaintain, bool isOn, int initialTemp)
: heatingUnit(new HeatingUnit(isOn, initialTemp)),
  tempToMaintain(tempToMaintain)
{ }

However, using new requires that you have to cleanup the memory yourself using delete so the following would be probably be preferable:

BangBangControl(int tempToMaintain, bool isOn, int initialTemp)
: heatingUnit(isOn, initialTemp),
  tempToMaintain(tempToMaintain)
{ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文