【问题标题】:Operator overloading definition outside of the class - C++类外的运算符重载定义 - C++
【发布时间】:2016-03-07 03:20:26
【问题描述】:

抱歉,这是一个如此简单的问题。我一直在使用 tutorialspoint.com 来学习 C++。目前我正在尝试了解类和运算符重载。对于其他成员函数,网站使用此约定

    class Box {
     private:
      int Volume;
     public:
      Box(int num);
      ...
     }
     Box::Box(int num) {
      Volume = num;
     }

但是,当重载运算符时,他们使用 this

     class Box {
      private:
       int volume;
      public:
       Box(int num);
       Box operator+(const Box &b) {
        Box box;
        box.volume = this->volume + b.volume;
        return box;
       }
      }

它们在类中定义了重载函数。是否可以在课堂之外定义它?如果是这样,怎么做?我试过了

     Box Box::operator+(const Box &b) {...}
     Box::Box operator+(const Box &b) {...}

但这些不起作用

我如何在课堂之外做到这一点?

再次抱歉,这是一个如此简单的问题。 谢谢

编辑 我的代码是这样的

#include <iostream>
#include <string>
using namespace std;

class Box {
 private:
  int volume;
 public:
  Box(int num);
  Box operator+(const Box &b);
};

Box::Box(int num) {
 volume = num;
}

Box Box::operator+(const Box &b) {
 Box box;
 box.volume = this->volume + b.volume;
 return box;
}

int main() {
 Box one(2);
 Box two;
 two = one + one;  
}

我的错误是

    Overloading.cc: In member function 'Box Box::operator+(const Box&)':
    Overloading.cc:18:6: error: no matching function for call to 'Box::Box()'
    Overloading.cc:18:6: note: candidates are:
    Overloading.cc:13:1: note: Box::Box(int)
    Overloading.cc:13:1: note:   candidate expects 1 argument, 0 provided
    Overloading.cc:5:7: note: Box::Box(const Box&)
    Overloading.cc:5:7: note:   candidate expects 1 argument, 0 provided
    Overloading.cc: In function 'int main()':
    Overloading.cc:25:6: error: no matching function for call to 'Box::Box()'
    Overloading.cc:25:6: note: candidates are:
    Overloading.cc:13:1: note: Box::Box(int)
    Overloading.cc:13:1: note:   candidate expects 1 argument, 0 provided
    Overloading.cc:5:7: note: Box::Box(const Box&)
    Overloading.cc:5:7: note:   candidate expects 1 argument, 0 provided

【问题讨论】:

  • 第一个应该没问题。你是什​​么意思“不工作”?有任何错误信息吗?
  • 当您说“这些不起作用”时,您的意思是什么?你有构建错误吗?出乎意料的结果?还有什么?请详细说明,并向我们提供更多详细信息(包括构建错误,如果有的话)。

标签: c++ class overloading


【解决方案1】:

是的,尽可能在类之外定义运算符是可能的,而且很常见。 你想要这个:

Box operator+( const Box& left, const Box& right ) { ... }

编辑:

如果您的意思是要将运算符声明为成员函数并在其他地方(在 cpp 中)定义它,那么它是:

h

class Box
{
    Box operator+( const Box& other ) const;
};

cpp

Box Box::operator+( const Box& other ) const { ... }

编辑 2: 现在你已经发布了你的错误。您在创建 Box two; 时尝试使用默认构造函数但是,由于您创建了非默认构造函数 Box( int ),编译器不会为您生成默认构造函数。

【讨论】:

  • 我认为 OP 的意思是“成员函数”。
  • 谢谢。我认为如果未定义默认构造函数,编译器会自行将成员变量设置为其默认值。例如所有整数都默认为 0,所有指针都指向 NULL,等等。
【解决方案2】:

错误信息与运算符重载无关,它只是抱怨Box 没有声明Box box;Box two; 的默认构造函数(即Box::Box())。

您可以为其添加一个默认构造函数,例如:

class Box {
 private:
  int volume;
 public:
  Box() : volume(0) {}
  ~~~~~~~~~~~~~~~~~~~~
  Box(int num);
  Box operator+(const Box &b);
};

或更改语句以指定参数,如:

Box box(0);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-17
    • 2012-11-26
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多