Results 1 to 1 of 1
So I'm writing a program for my class and have been working on a windows machine until I got home today and tried to run the program on my linux ...
- 03-07-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 1
<< >> Overload problem with g++
So I'm writing a program for my class and have been working on a windows machine until I got home today and tried to run the program on my linux machine. From what I have found online, g++ seems to have a problem overloading I/O operators. Is there a way to get around this?
Here are just a few errors that I am getting that I wasn't getting before..
And here is my code/Code:Main.cpp: In function ‘int main()’: Main.cpp:354: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::ostream*)operator<<(((std::ostream&)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)((std::ostream*)operator<<(((std::ostream&)((std::basic_ostream<char, std::char_traits<char> >*)std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Number1:")))), ((Rational&)(& ratNum1))))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](std::endl [with _CharT = char, _Traits = std::char_traits<char>]))), ((const char*)"Number2:")))), ((Rational&)(& ratNum2))))->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](std::endl [with _CharT = char, _Traits = std::char_traits<char>]))), ((const char*)"Result + operator=")) << operator+(((Rational&)(& ratNum1)), ((Rational&)(& ratNum2)))’ /usr/include/c++/4.3/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:131: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:177: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/bits/ostream.tcc:97: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:184: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/bits/ostream.tcc:111: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:195: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:208: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:217: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/ostream:229: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.3/bits/ostream.tcc:125: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>] Main.cpp:57: note: std::ostream& operator<<(std::ostream&, Integer&) Main.cpp:161: note: std::ostream& operator<<(std::ostream&, Rational&) Main.cpp:272: note: std::ostream& operator<<(std::ostream&, Complex&)
I hope that helps. Thanks!!Code:#include <iostream> //#include "Integer.h" //#include "Rational.h" //#include "Complex.h" using namespace std; /***********************Integer.h****************************/ /* Integer class holds numbers in the format: a */ /* This class then takes traditional operators, overloads */ /* them that allows the main program to perform addition and*/ /* multiplication as well as print our results out in a */ /* clear format. */ /************************************************************/ class Integer { friend const int operator +(Integer& x, Integer& y); // Function overloads the + operator to work with objects of Integer type friend const int operator *(Integer& x, Integer& y); // Function overloads the * operator to work with objects of Integer type friend ostream& operator <<(ostream& outputStream, Integer& a); // Overloads the ostream operator << to print all information of Integer type objects friend istream& operator >>(istream& inputStream, Integer& w); // Overloads the istream operator >> to take direct input of Integer type objects private: int a; // main number for Integers, the numerator for Rationals, and the leading coefficient for Complex public: Integer(); Integer(int); int getA(); void setA(int); }; /************************Addition****************************/ /* This function overloads the + opperator so that you can */ /* perform traditional addition using the numerical format */ /* of this class. */ /************************************************************/ const int operator +(Integer& x, Integer& y) { int a = x.getA() + y.getA(); return a; } /*********************Multiplication*************************/ /* This Function overloads the * opperator so that you can */ /* perform traditional multiplication using the numerical */ /* format of this class. */ /************************************************************/ const int operator *(Integer& x, Integer& y) { int a = x.getA() * y.getA(); return a; } /*********************outputStream***************************/ /* This function overloads the outputStream << opperator so */ /* that you can perform outputs of Integer objects quickly */ /* and efficiently. */ /************************************************************/ ostream& operator <<(ostream& outputStream, Integer& a) { outputStream << a.getA(); return outputStream; // for multiple << operators. } /**********************inputStream***************************/ /* This function overloads the inputStream >> opperator so */ /* that you can perform input of data into a Integer object */ /* quickly and efficiently */ /************************************************************/ istream& operator >>(istream& inputStream, Integer& a) { int w; inputStream >> w; a.setA(w); return inputStream; } /******************Integer Constructor***********************/ /* This constructor opperates to initialize a basic Integer */ /* object. */ /************************************************************/ Integer::Integer() { a = 0; } /******************Integer Constructor***********************/ /* This constructor opperates to initialize specific */ /* Integer objects, with a specific set of input from the */ /* main. */ /************************************************************/ Integer::Integer(int x) { a = x; } /**********************Integer getA**************************/ /* This function returns the value of your 'a' variable */ /************************************************************/ int Integer::getA() { return a; } /***********************Integer setA*************************/ /* This function can set the value of your 'a' variable. */ /************************************************************/ void Integer::setA(int x) { a = x; } /*********************************************************/ class Rational: public Integer { friend Rational operator +(Rational& x, Rational& y); // Function overloads the + operator to work with objects of Rational type friend Rational operator *(Rational& x, Rational& y); // Function overloads the * operator to work with objects of Rational type friend ostream& operator <<(ostream& outputStream, Rational& b); // Overloads the ostream operator << to print all information of Rational type objects friend istream& operator >>(istream& inputStream, Rational& b); // Overloads the istream operator >> to take direct input of Rational type objects private: int b; // denominator public: Rational(); Rational(int, int); int getB(); void setB(int); }; /************************Addition****************************/ /* This function overloads the + opperator so that you can */ /* perform traditional addition using the numerical format */ /* of this class. */ /************************************************************/ Rational operator +(Rational& x, Rational& y) { int numx, numy, den; numx = x.getA() * y.getB(); numy = y.getA() * x.getB(); den = x.getB() * y.getB(); Rational a(numx+numy, den); return a; } /*********************Multiplication*************************/ /* This Function overloads the * opperator so that you can */ /* perform traditional multiplication using the numerical */ /* format of this class. */ /************************************************************/ Rational operator *(Rational& x, Rational& y) { int a = x.getA() * y.getA(); int b = x.getB() * y.getB(); Rational c(a, b); return c; } /*********************outputStream***************************/ /* This function overloads the outputStream << opperator so */ /* that you can perform outputs of Rational objects quickly */ /* and efficiently. */ /************************************************************/ ostream& operator <<(ostream& outputStream, Rational& b) { if(b.getA()>0 && b.getB()>0 || b.getA()<0 && b.getB()<0) outputStream << "+" << b.getA() << "/" << b.getB(); else if(b.getB()<0) outputStream << -b.getA() << "/" << -b.getB(); else outputStream << b.getA() << "/" << b.getB(); return outputStream; } /**********************inputStream***************************/ /* This function overloads the inputStream >> opperator so */ /* that you can perform input of data into a Rational object*/ /* quickly and efficiently */ /************************************************************/ istream& operator >>(istream& inputStream, Rational& b) { int x, y; char bs; inputStream >> x >> bs >> y; Rational a(x, y); b = a; return inputStream; } /*****************Rational Constructor***********************/ /* This constructor opperates to initialize a basic Rational*/ /* object. */ /************************************************************/ Rational::Rational() :Integer() { int b = 0; } /*****************Rational Constructor***********************/ /* This constructor opperates to initialize specific */ /* Rational objects, with a specific set of input from the */ /* main. */ /************************************************************/ Rational::Rational(int x, int y) :Integer(x) { b = y; } /**********************Rational getB*************************/ /* This function returns the value of your denominator */ /************************************************************/ int Rational::getB() { return b; } /**********************Rational setB*************************/ /* This function can set the denominators value. */ /************************************************************/ void Rational::setB(int x) { b = x; } class Complex: public Integer { friend Complex operator +(Complex& x, Complex& y); // Function overloads the + operator to work with objects of Complex type friend Complex operator *(Complex& x, Complex& y); // Function overloads the * operator to work with objects of Complex type friend ostream& operator <<(ostream& outputStream, Complex& b); // Overloads the ostream operator << to print all information of Complex type objects friend istream& operator >>(istream& inputStream, Complex& b); // Overloads the istream operator >> to take direct input of Complex type objects private: int b; // Imaginary coefficient public: Complex(); Complex(int, int); int getB(); void setB(int); }; /************************Addition****************************/ /* This function overloads the + opperator so that you can */ /* perform traditional addition using the numerical format */ /* of this class. */ /************************************************************/ Complex operator +(Complex& x, Complex& y) { int numx, numy; numx = x.getA() + y.getA(); numy = x.getB() + y.getB(); Complex a(numx, numy); return a; } /**********************Subtraction***************************/ /* This function overloads the - opperator so that you can */ /* perform traditional subtraction using the numerical */ /* format of this class. */ /************************************************************/ Complex operator -(Complex& x, Complex& y) { int numx, numy; numx = x.getA() - y.getA(); numy = x.getB() - y.getB(); Complex a(numx, numy); return a; } /*********************outputStream***************************/ /* This function overloads the outputStream << opperator so */ /* that you can perform outputs of Complex objects quickly */ /* and efficiently. */ /************************************************************/ ostream& operator <<(ostream& outputStream, Complex& b) { if(b.getB()<0) outputStream << b.getA() << "-" << -b.getB() << "i"; else outputStream << b.getA() << "+" << b.getB() << "i"; return outputStream; } /**********************inputStream***************************/ /* This function overloads the inputStream >> opperator so */ /* that you can perform input of data into a Complex object */ /* quickly and efficiently */ /************************************************************/ istream& operator>>(istream& inputStream, Complex& b) { int x, y; char add, i; inputStream >> x >> add >> y >> i; Complex a(x, y); b = a; return inputStream; } /******************Complex Constructor***********************/ /* This constructor opperates to initialize specific */ /* Complex objects, with a specific set of input from the */ /* main. */ /************************************************************/ Complex::Complex(int x, int y) :Integer(x) { b = y; } /******************Complex Constructor***********************/ /* This constructor opperates to initialize a basic Complex */ /* object. */ /************************************************************/ Complex::Complex() :Integer() { b = 0; } /***********************Complex getB*************************/ /* This function returns the value of your Coefficient of i*/ /************************************************************/ int Complex::getB() { return b; } /***********************Complex setB*************************/ /* This function can set the Coefficient of i's value. */ /************************************************************/ void Complex::setB(int x) { b = x; } int main() { Integer intNum1(5), intNum2; cout << "Please enter an integer number:"; cin >> intNum2; //TA will enter different values to test your program. cout << "Number1:" << intNum1 << endl << "Number2:" << intNum2 << endl << "Result + operator=" << (intNum1+intNum2) << endl << "Result * operator=" << (intNum1*intNum2) << endl << endl; Rational ratNum1(1,2), ratNum2; cout << "Please enter a rational number:"; cin >> ratNum2;//TA will enter different values to test your program. cout << "Number1:" << ratNum1 << endl << "Number2:" << ratNum2 << endl << "Result + operator=" << (ratNum1+ratNum2) << endl << "Result * operator=" << (ratNum1*ratNum2) << endl << endl; Complex cmpNum1(1,-2), cmpNum2; cout << "Please enter a complex number:"; cin >> cmpNum2;//TA will enter different values to test your program. cout << "Number1:" << cmpNum1 << endl << "Number2:" << cmpNum2 << endl << "Result + operator=" << (cmpNum1+cmpNum2) << endl << "Result * operator=" << (cmpNum1-cmpNum2) << endl << endl; //#system("pause"); return 0; }


Reply With Quote