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.