gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:57:44 24 4
gpt4 key购买 nike

我已经非常努力地解决了这个问题。我一直都能通过谷歌找到我的答案,这是我第一次因为尽职调查而在论坛上发帖。然而,这完全难住了我,谷歌似乎在推荐 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com