[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

How do I overload the << operator in C++?

I would like to overload the << operator to display a polynomial.  This for my C++ class as I'm still still learning, I looked at the code from my previous semesters work to get the code that I listed in the code area.  I'v also attached a zip file with my the header and both cpp files in text format.

poly is an array of 10 integers.  

any light any of you can shed on ths will be appreciated.



 I am getting the following errors when I try to compile them in VS8.


Error      1      error C2143: syntax error : missing ';' before '&'      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      2      error C2433: 'ostream' : 'friend' not permitted on data declarations      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      3      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      4      error C2061: syntax error : identifier 'ostream'      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      5      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      6      error C2805: binary 'operator <<' has too few parameters      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      7      error C2872: 'ostream' : ambiguous symbol      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      8      error C2143: syntax error : missing ';' before '&'      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      9      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      10      error C2086: 'int ostream' : redefinition      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      11      error C2872: 'ostream' : ambiguous symbol      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      12      error C2872: 'ostream' : ambiguous symbol      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      13      error C2065: 'strm' : undeclared identifier      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      14      error C2059: syntax error : 'const'      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      81      cs110clab2
Error      15      error C2143: syntax error : missing ';' before '{'      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      82      cs110clab2
Error      16      error C2447: '{' : missing function header (old-style formal list?)      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.cpp      82      cs110clab2
Error      17      error C2143: syntax error : missing ';' before '&'      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      18      error C2433: 'ostream' : 'friend' not permitted on data declarations      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      19      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      20      error C2061: syntax error : identifier 'ostream'      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      21      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2
Error      22      error C2805: binary 'operator <<' has too few parameters      c:\users\carlos\documents\homework\cs110c\cs110clab2\cs110clab2\poly.h      21      cs110clab2




#ifndef POLY_H
#define POLY_H
//I read that the the overload has to be outside the class, however i get other errors when
//I uncommen these.
//class poly;
 //ostream &operator << (ostream &, const poly &);
 
 class poly
 {
 private:
	 int polynomial[10];
 
 public:
	 poly();
	 ~poly();
	 void setPoly();
	 void changeCoefficient(int, int);
	 bool breakOutLoop(int, int);
	 void degree();
	 int coefficient(int);
	 poly operator +( poly&);
 
// I've tried removing spaces, adding spaces but it has not worked and the errors point to this
 //line as well as the line within the .cpp.
 
	 friend ostream & operator << (ostream &strm, const poly &obj);
	 
 };
 
#endif
 
with the the cpp i have
 
ostream & operator << (ostream &strm,  const poly &obj)
{
return strm;
}

Open in new window

prog.txt.zip
0
cflores89
Asked:
cflores89
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
actually, this should do:
 friend ostream& operator<<(ostream &strm, const poly &obj);

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
of course, with the implementation:
friend ostream& operator<<(ostream &strm, const poly &obj)
{
  return strm;
}

Open in new window

0
 
cflores89Author Commented:
no joy, same errors ;(
0
 
vhpgomesCommented:
Apparently you are missing the header files, and namespace declaration.

did included <iostream> somewhere ?

and prepend std:: to all 'ostream' , like:




// ...
 
#include <iostream>
 
// ...
 
friend std::ostream& operator<<(std::ostream &strm, const poly &obj);

Open in new window

0
 
cflores89Author Commented:
Bingo!!
Thank you!  I could have looked it at forever and not have figured it out!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now