non-reference while << operator overloading

I am writing a class matrix for practice but a situation have come to make me a problem, see.

I want to do is

cout << (mymatrix + 5.0);

but my related overloaded operators are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ostream & operator <<(ostream & os, matrix & m){		//overloaded << to enable console output of matrix object
	m.console(os);							//use already class defined method to output matrix on console
	return os;								//<< must return ostream object to enable casecading
}//----------------operator <<

matrix matrix::operator +(double scalar) const{		//overloaded + to enable addition of a matrix with a scalar
	matrix temp(totalrows, totalcols);
	for(int rows = 0; rows < totalrows; rows++){	//nested loop to process element by element keep adding scalar element by
//element to values obtained from current matrix and keep storing results in temporary matrix
		for(int cols = 0; cols < totalcols; cols++){
			temp.thematrix[rows][cols] = (this->thematrix[rows][cols] + scalar);
		}//----------------for
	}//--------------for
	return temp;
}//-------------operator + (double) 


cout << mymatrix; works fine

1
2
3
matrix newmat(m2.getRows(), m2.getCols());
newmat = (mymatrix + 5.0);
cout << newmat;
works fine too (I've overloaded = operator (with self-assignment test) too)

but cout << (mymatrix + 5.0); does not work and I know because I need to return reference according to my overloaded << operator but if I do that in overloaded + operator then I just can't do because I can't return a local variable by reference from within + overloaded operator

I can do one more thing overload << operator to take object matrix instead of taking a reference to matrix but then I get some kind of linker error

Please tell me what is the original solution to this problem?
The solution is to make operator<< take a reference to a const matrix.

 
ostream & operator <<(ostream & os, const matrix & m)
if I put const in prototype and definition of << operator overloading then compiler/linker gives an error and shows that error in Makfile.win file
also what if I didn't declare my matrix as constant like

matrix m;
const matrix m2;
now if function receives type "const matrix &" then it can't receive "m" and if it receives "matrix &" then it can't receive "m2"

and in most of my program I'm using matrix objects which are not constant
If your matrix class operator << needs to access private members of the matrix class you need to declare it as a friend of the matrix class:

1
2
3
4
5
friend ostream& operator <<(ostream& os, constr matrix& m)
{
   // do your stuff
   return os;
}


HTH
What error?

Make sure matrix::console is marked const.
A reference to a const can refer to non-const objects just fine. The opposite is not true.

The reason you need it to be const is because when you do (mymatrix + 5.0) this will return a temporary object. A temporary object will not bind to a reference that refers to a non-const object.
void console(ostream &) const; //to output matrix on console this is prototype

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void matrix::console(ostream & os) const{				//to output matrix on console using an ostream object
	os.setf(ios::showpoint);							//show decimal point
	os.setf(ios::fixed, ios::floatfield);				//fixed notation (opposite to scientific)
	
	os << endl << "order(" << totalrows << " X " << totalcols << ")" << endl;			//matrix order infromation
	
	os << (char)218;												//first row starting character
	for(int i = 0; i < totalcols; i++) os << setw(10) << " ";		//a blank row
	os << (char)191 << endl;										//first row ending character
	
	for(int rows = 0; rows < totalrows; rows++){					//output matrix upto 2nd last row
		os << (char)179;								//current row starting character
		for(int cols = 0; cols < totalcols; cols++) os << setprecision(2) << setw(10) << thematrix[rows][cols];
//values of current row of matrix
		
		os << (char)179 << endl;						//current row ending character
	}//--------------for
	
	os << (char)192;												//last row starting character
	for(int i = 0; i < totalcols; i++) os << setw(10) << " ";		//a blank row
	os << (char)217 << endl;										//last row ending character
	
	os << std::resetiosflags(std::cout.flags());					//reset received ostream object to it's default
}//------------console() 
this is definition

where to put const ???
Ok, so you have it marked as const already. Can you post the error message you get?
compiles fine with this type
friend ostream & operator <<(ostream &, matrix &);both prototype+definition except definition has identifier names

but when I change to following
friend ostream & operator <<(ostream &, const matrix &); there is error

E:\Education\cpp\Final Exercise\main.o main.cpp:(.text+0x21e): undefined reference to `operator<<(std::ostream&, matrix&)'
E:\Education\cpp\Final Exercise\main.o main.cpp:(.text+0x25e): undefined reference to `operator<<(std::ostream&, matrix&)'
E:\Education\cpp\Final Exercise\main.o main.cpp:(.text+0x29e): undefined reference to `operator<<(std::ostream&, matrix&)'
E:\Education\cpp\Final Exercise\main.o main.cpp:(.text+0x357): undefined reference to `operator<<(std::ostream&, matrix&)'
E:\Education\cpp\Final Exercise\main.o main.cpp:(.text+0x407): undefined reference to `operator<<(std::ostream&, matrix&)'
c:\program files (x86)\dev-cpp\mingw64\x86_64-w64-mingw32\bin\ld.exe main.o: bad reloc address 0x1 in section `.text$_ZStorSt13_Ios_OpenmodeS_[_ZStorSt13_Ios_OpenmodeS_]'
E:\Education\cpp\Final Exercise\collect2.exe [Error] ld returned 1 exit status
25 E:\Education\cpp\Final Exercise\Makefile.win recipe for target 'Matrix-Project.exe' failed


also indicate error in following file named "Makefile.win"

# Project: Matrix-Project
# Makefile created by Dev-C++ 5.6.3

CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe
OBJ = main.o matrix.o
LINKOBJ = main.o matrix.o
LIBS = -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib" -L"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib" -static-libgcc -pg
INCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include"
CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include" -I"C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.8.1/include/c++"
BIN = Matrix-Project.exe
CXXFLAGS = $(CXXINCS) -pg
CFLAGS = $(INCS) -pg
RM = rm.exe -f

.PHONY: all all-before all-after clean clean-custom

all: all-before $(BIN) all-after

clean: clean-custom
${RM} $(OBJ) $(BIN)

$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS) //error on this line

main.o: main.cpp
$(CPP) -c main.cpp -o main.o $(CXXFLAGS)

matrix.o: matrix.cpp
$(CPP) -c matrix.cpp -o matrix.o $(CXXFLAGS)
Oh no, it got compiled, I just hit the "Rebuild all" button and after that it compiled perfect now let me see if my OP problem is solved?


and yes that is solved too. Could you please explain what I were doing wrong? I'm very thankful to you.
Topic archived. No new replies allowed.