接收错误“类型‘%T’无效的操作数”和“%T”到二进制“%O”在评估“%Q(%#T, %#T)”时”
我已经非常非常努力地尝试解决这个问题。我一直能够通过谷歌找到我的答案,这是我第一次因为我的尽职调查而在论坛上发帖。然而,这完全难住了我,谷歌似乎推荐 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*)’
我一直在发疯,寻找我试图用作 + 基础的函数中是否使用了“+”,然后更改了各种指针等等,然后把它放在课堂之外班级内。
明天,我可能会参考运算符重载的一般规则的规则#1,但今天我'我花了 6 个小时来完成原本需要四分钟才能验证其正常工作并继续前进的事情……我是一只非常悲伤的熊猫。
I have tried very very hard to solve this problem. I have always been able to find my answer through Google, and this is the first time I have ever posted to a forum because of my due diligence. However, this has completely stumped me, and Google appears to be recommending the source code for gcc, which suggests to me that this problem is a rarity.
Here is my simplified code:
#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;
}
I created a large list of comparison functions (I'm trying to recreate the string class for unsigned characters, if someone knows of something that exists for this, that would be swell!) and they all worked perfectly, but the error I've been getting for this is:
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*)’
and I have been going nuts, looking for if I used a '+' inside the function I was trying to use as the basis for the +, and then changing all kinds of pointers and whatnot, and putting it outside the class and inside the class.
Tomorrow, I may refer to rule #1 of the General rules of Operator overloading but today I've spent 6 hours on something that was supposed to take four minutes to verify it was working and move forward... I am a very sad panda.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
运算符重载必须至少有一个属于用户定义类型的参数。您的代码是:
c_a
和c_b
都是指针,不是用户定义的类型。Operator overloads must have at least one argument that is a user-defined type. Your code is:
Both
c_a
andc_b
are pointers, which are not user-defined types.修复应该是:
C++ 中的运算符不是“提升”的(就像在函数式语言中一样,例如 Haskell;C# 具有 可空类型的提升运算符)
注意 @Daniel:此答案引用了您问题的答案(“为什么应该这样”):C++ 不为指针执行自动提升运算符。我想这可能主要是因为 C++ 希望在 指针方面保持 C 兼容改为算术。
The fix should be:
Operators in C++ are not 'lifted' (like in functional languages, like e.g. Haskell; C# has the notion of lifted operator for Nullable types)
Note @Daniel: This answer references the answer to your question ("why it should be this way"): C++ doesn't do automatic lifted operators for pointers. I guess this is probably mainly because C++ wants to stay C-compatible with respect to pointer arithmetic instead.
我得到了类似的
错误:类型“int64_t {aka long int}”和“”到二进制“operator*”的操作数无效
但出于与OP不同的原因。
对我来说,我试图将一个值乘以“索引”,该值在函数中进一步声明。
可悲的是,它似乎在实际错误部分之前抱怨函数的参数。删除“*索引”
解决了这个问题。我猜是无用的错误消息。
I got the similar
error: invalid operands of types ‘int64_t {aka long int}’ and ‘’ to binary ‘operator*’
but for a different reason than OP.
For me I was trying to multiply a value by "index" which was declared further down in the function.
Sadly, it seemed to be complaining about parameters to the function from before the actual bad part. Removing the "* index" from
fixed the problem. Unhelpful error message I guess.