Compiling errors, unsure of whats wrong

The code I have works fine in the Visual compiler, but will not compile in g++. Its 3 files. The header file, the main , and another... the error that keeps appearing is "invalid initialization of non-const ref"....

I will post the header and definition class, any help is appreciated as I am a novice programer


Header:

#include<iostream>
#include<fstream>

using namespace std;

#ifndef DOCUMENT_H
#define DOCUMENT_H

class Document{


friend ostream& operator<<(ostream&, Document&);
friend istream& operator>>(istream&, Document&);

// private variables not accessible to the client
private: char *ptr;
int data;
static const int size = 5000;
public:
Document(); //Default
Document(Document&); // copy constructor
int countWords();
int length();
Document operator=(Document&);
Document operator=(const string&);
char operator[](int);
Document operator+(Document&);

};

#endif

Definition class


#include"Document.h"

using namespace std;

//defualt constructor creating dynamic array and setting its size, as well as setting length to zero
Document::Document(){

ptr = new char[size];
ptr[0] = '\0';
data = 0;

}


Document::Document(Document& d1){

ptr = new char[size];
data = d1.length();

for (int i = 0; i < data; i++){
ptr[i] = d1[i];

}
ptr[data] = '\0';

}

int Document::countWords(){
int counter = 0;


for (int i = 0; i < data; i++){


if (ptr[i] <= ' ' || ptr[i] == ',' || ptr[i] == ';' || ptr[i] == ':' || ptr[i] == '-' || ptr[i] == '.'){

counter++;

while (ptr[i] <= ' ' || ptr[i] == ',' || ptr[i] == ';' || ptr[i] == ':' || ptr[i] == '-' || ptr[i] == '.')

i++;
}

}

if (ptr[data - 1] != ' ' || ptr[data - 1] != ',' || ptr[data - 1] != ';' || ptr[data - 1] != ':' || ptr[data - 1] != '-' || ptr[data - 1] != '.')

counter++;

return counter;

}


int Document::length(){

int count = 0;

while (ptr[count] != '\0'){

count++;
}

return count;


}

Document Document::operator=(Document& d1){


data = d1.length();

for (int i = 0; i < data; i++)
ptr[i] = d1[i];

ptr[data] = '\0';

return *this;

}


Document Document::operator=(const string& d1){

data = d1.length();

for (int i = 0; i < data; i++)

ptr[i] = d1[i];

ptr[data] = '\0';

return *this;


}

ostream& operator<<(ostream& out, Document& d1){

int data = d1.length();
for (int i = 0; i < data; i++)
out << d1[i];

return out;

}

istream& operator>>(istream &in, Document &d1){



in.getline(d1.ptr, d1.size, '\0');
return in;
}

char Document::operator[](int count){

return ptr[count];


}

Document Document::operator+(Document& d1){

Document d2 = (*this);

int trial = d1.length();
int oldLength = d2.length();


//for (int i = 0; i < oldLength; i++){
// d2.ptr[i] = (*this)[i];
// }

for (int i = 0; i < trial; i++){
d2.ptr[i + oldLength] = d1[i];
}
trial = trial + oldLength;
d2.ptr[trial] = '\0';
return d2;
}
Hi,

What was the entire command you used to compile with g++ ?

If you have errors please post them here verbatim.

Please use code tags: http://www.cplusplus.com/articles/z13hAqkS/

Good Luck !!
the entire command was g++ prog6.cpp document.cpp -o prog6

the errors that I got are all on the + operator overload fcn and they state

prog6.cpp:38:14: error: invalid initialization of non-const reference of type ‘D ocument&’ from an rvalue of type ‘Document’
doc2 = doc2 + doc1;
Hi,

It would be easier if you edited your post, select the code, then press the <> button. This will use code tags. Then we get line numbers, keywords and can compile it right here with cpp.sh - the gear icon top right of the code snippet.

You should compile with a high level of warnings and with the latest standard, as a minimum:

g++ -std=c++14 - Wall -Wextra -pedantic-errors *.cpp -o prog6

If your compiler can't do c++14, then try try c++11. Or it's easy to upgrade to the latest gcc.

There are even more warnings that aren't turned on even with the above options:
http://www.cplusplus.com/forum/general/183731/#msg899203

Can we have your file with main in it, then I can compile myself?

As well can you provide all the error warnings?

A link to operator+ http://en.cppreference.com/w/cpp/language/operators their implementation is different to yours.

You could also implement operator+=




Thank you all, but with the help of a friend I did figure it out, HOWEVER, I will in the future use the coding blocks as was asked of me. Again thank you!
Hi,

Just out of interest, what was the problem? The way you wrote the function?
I apologize, I did not see this point until now, it was partly the fcn and the other part was the I actually forgot to bring the text file I was trying to read over into Linux as well....
Topic archived. No new replies allowed.