I'm trying to overload the <<
operator on the ostream
- class?
For some reason I'm overloading it twice, I can't seem to figure out why cause i have #ifndef
in my header file.
matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
class matrix {
int x, y;
public:
matrix(int a, int b);
matrix& operator* (matrix B);
friend ostream& operator<< (ostream& os, const matrix& A);
};
ostream& operator<< (ostream& os, const matrix& A)
{
os << "Matrix.....";
return os;
}
#endif
matrix.cpp
#include <iostream>
#include "matrix.h"
matrix::matrix(int a, int b) {
}
matrix& matrix::operator* (matrix B) {
}
and main.cpp
#include <iostream>
#include "matrix.h"
using namespace std;
int main () {
matrix a(6, 6), b(6, 6);
cout << a;
return 0;
}
I am building like this:
$ cat build.sh
g++ -c main.cpp
g++ -c matrix.cpp
g++ -g -o main main.o matrix.o
The build error I am getting is:
$bash build.sh
ld: duplicate symbol operator<<(std::basic_ostream<char, std::char_traits<char> >&, matrix const&)in matrix.o and main.o for architecture x86_64
collect2: ld returned 1 exit status
Think this is strait forward but I can't seem to find the solution.
Thanks for your time.
g++ -v
$g++ -v
...skipped 4 lines...
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)