私は非常に難しいこの問題。私はいつもGoogleを通して私の答えを見つけることができました。これは私のデュー・ディリジェンスのためにフォーラムに投稿した初めてのことです。しかし、これは完全に私を困らせてしまいました.Googleはsource code for gccを推奨しているようです。この問題はまれです。ここで`%Q(%#T、%#T) 'の評価で、タイプ'%T 'とタイプ'%T 'の無効なオペランドがバイナリ`%O'に出力されています "
は私の簡素化コードです:
#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を参照するかもしれませんが、今日私はそれが働いていて、前進していることを確認するために4分かかると思われる何かに6時間を費やしました...私は非常に悲しいパンダです。
ああ神ああ神ああ神...完全に投稿の14秒以内にこれを解決...それがために叫んでたので、それは私が C_A + = C_B * としてC_A + = * C_B を投稿したかった、それはこのようでなければなりませんなぜ私は完全には理解していないが、それはのように一日として明らかになりました私はそのエラーをもう一度見てすぐに...私はこれを私の馬鹿馬鹿めしさの証言として残しておきます(これに費やした6時間の冗談はなく、6時間15秒過ごしたはずです...)他の誰かがポインタの数学101でこのエラーにつまずくはずです。 –
ああ、それはおそらく本当です。これは私のクラスのテストベンチの一部でしたので、DJPcharオブジェクトは動的に割り当てられ、他のオブジェクトに追加されています。コードのその部分が削除されている可能性があります。 –
"符号なし文字の文字列クラスを再作成する" - 基本的に 'std :: basic_string'を探していますか? –
MSalters