c++ - 在评估 "invalid operands of types ` 时接收错误 `%T' %T' 和 `%O' 到二进制 `%Q(%#T, %#T)'“

标签 c++ operator-overloading

我已经非常努力地解决了这个问题。我一直都能通过谷歌找到我的答案,这是我第一次因为尽职调查而在论坛上发帖。然而,这完全难住了我,谷歌似乎在推荐 source code for gcc ,这表明这个问题很少见。

这是我的简化代码:

#include <sstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class DJPchar{
    public:
    //General Variables
        unsigned char *word;
        int length;

    //Initialization Functions
    DJPchar();
    DJPchar(unsigned char *w, int len);
    ~DJPchar();

    DJPchar& operator+=(const DJPchar& word2);
    };

/////////////////////////////////////////////////////////////
//Initialization Functions
/////////////////////////////////////////////////////////////
DJPchar::DJPchar(){
    word = NULL;
    length = 0;
    }

DJPchar::DJPchar(unsigned char *w, int len){
    int i;

    length = len;
    word = new unsigned char[length];
    for (i=0; i<length; i++){
        word[i] = w[i];
        }
    }

DJPchar::~DJPchar(){
    delete[] word;
    }

/////////////////////////////////////////////////////////////
//Problem Function
/////////////////////////////////////////////////////////////

DJPchar& DJPchar::operator+=(const DJPchar &word2){
    unsigned char *temp;
    int i, newlength;

    temp = this->word;
    newlength = this->length + word2.length;

    this->word = new unsigned char (newlength);

    for(i=0; i<this->length; i++){
        this->word[i] = temp[i];
        }
    for(i=this->length; i<newlength; i++){
        this->word[i] = word2.word[i-this->length];
        }

    this->length = newlength;
    delete[] temp;

    return *this;
    }

int main(){
    unsigned char a;
    unsigned char b[7];

    a = 'b';
    b[0] = 'b';
        b[1] = 'a';
        b[2] = 't';
        b[3] = '\n';
        b[4] = 200;
        b[5] = 'n';
        b[6] = '!';

    DJPchar *c_a = new DJPchar(&a, 1);
    DJPchar *c_b = new DJPchar(b, 7);

    c_a += c_b; //Error Line

    return 0;
}

我创建了大量比较函数(我正在尝试为无符号字符重新创建字符串类,如果有人知道为此存在的东西,那就太棒了!)并且它们都工作得很好,但是错误我得到的是:

Broken.cpp:86: error: invalid operands of types ‘DJPchar*’ and ‘DJPchar*’ to binary ‘operator+’
Broken.cpp:86: error:   in evaluation of ‘operator+=(class DJPchar*, class DJPchar*)’

我一直在发疯,寻找我是否在我试图用作 + 基础的函数中使用了“+”,然后更改各种指针和诸如此类的东西,并将其放在类之外和类内。

明天,我可能会引用 General rules of Operator overloading 规则 #1但是今天我花了 6 个小时来做​​一些本应花 4 分钟来验证它是否有效并继续前进的事情……我是一只非常悲伤的 Pandas 。

最佳答案

运算符重载必须至少有一个参数是用户定义的类型。您的代码是:

DJPchar *c_a = new DJPchar(&a, 1);
DJPchar *c_b = new DJPchar(b, 7);

c_a += c_b; //Error Line

c_ac_b都是指针,不是用户自定义类型。

关于c++ - 在评估 "invalid operands of types ` 时接收错误 `%T' %T' 和 `%O' 到二进制 `%Q(%#T, %#T)'“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7443457/

相关文章:

c++ - 您如何确定 C++ 中 Linux 系统 RAM 的数量?

javascript - Javascript 中数组的重载 [] 运算符

c++ - 运算符重载使用operator +作为类模板

c++ - 适用于 native Visual Studio 开发人员的有用加载项或插件

c++ - 如何在程序执行期间防止屏幕保护程序和 sleep ?

c++ - OpenCV 机器学习函数需要 CvFileStorage* 而不是 cv::FileStorage*

matlab - 为什么 Matlab R2010 不能从 R2007 加载神经网络对象?

C++ 运算符重载 : implementing + with +=

C++ 类继承和运算符重载;运算符重载会被继承吗?

c++ - C/C++ : adding 0