473,503 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do I get "error: declaration of ‘operator<<’ as non-function"?

1 New Member
The following code won't compile with g++, instead giving me the error:
-----------------
prog2.cpp:36:33: error: declaration of ‘operator<<’ as non-function
prog2.cpp:36:32: error: expected ‘;’ before ‘(’ token
prog2.cpp: In function ‘int main()’:
prog2.cpp:49:12: error: no match for ‘operator<<’ in ‘std::cout << z’
------------------

My code is below, it is a simple complex number class:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class complex
  5. {
  6. private:
  7.    double re, im; //real and imaginary parts
  8. public:
  9.    void set(double r, double i) {re = r; im = i;}
  10.    complex() {set(0.0,0.0);}
  11.    complex(double r, double i) {set(r,i);}
  12.    complex(const complex &src)
  13.       {re = src.re; im = src.im;}
  14.    complex(double r) {set(r,0.0);}
  15.  
  16.    double get_re() const {return re;}
  17.    double get_im() const {return im;}
  18.    complex get_conj() const
  19.       {complex c(re, -1.0*im); return c;}
  20.    double get_mag_sq() const
  21.       {double m = (re*re)+(im*im); return m;}
  22.  
  23.    complex add(const complex &other)
  24.       {complex c(re+other.re,im+other.im); return c;}
  25.    complex sub(const complex &other)
  26.       {complex c(re-other.re,im-other.im); return c;}
  27.    complex mult(const complex &other);
  28.    complex div(const complex &other);
  29.  
  30.    complex operator+(const complex &other) {return add(other);}
  31.    complex operator-(const complex &other) {return sub(other);}
  32.    complex operator*(const complex &other) {return mult(other);}
  33.    complex operator/(const complex &other) {return div(other);}
  34.    bool operator==(const complex &other)
  35.       {return re == other.re && im == other.im;}
  36.    friend ostream &operator<<(osteam &os, complex &cplx);
  37. };
  38.  
  39. int main()
  40. {
  41.    complex x(5.0,6.9);
  42.    complex y(3.0,4.0);
  43.    complex z = x*y;
  44.    z = z/y;
  45.  
  46.    cout << x.get_re() << "+" << x.get_im() << "i" << endl;
  47.    cout << z.get_re() << "+" << z.get_im() << "i" << endl;
  48.    cout << (x == y) << endl;
  49.    cout << z << endl;
  50.  
  51.    return 0;
  52. }
  53.  
  54. complex complex::mult(const complex &other)
  55. {
  56.    complex c(re*other.re-im*other.im,re*other.im+im*other.re);
  57.    return c; 
  58. }
  59.  
  60. complex complex::div(const complex &other)
  61. {
  62.    complex c = (*this * other.get_conj())*(1/other.get_mag_sq());
  63.    return c;
  64. }
  65.  
  66. ostream &operator<<(ostream &os, complex &cplx)
  67. {
  68.    os << cplx.re << "+" << cplx.im << "i";
  69.    return os;
  70. }
  71.  
Jul 27 '11 #1
1 9781
weaknessforcats
9,208 Recognized Expert Moderator Expert
The friend declaration has misspelled ostream as osteam.
Jul 28 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2451
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
1
1988
by: ian.davies52 | last post by:
I'm having a problem running a query. I get the "too many fields" error message, but I only have 162 fields in the query and I thought the limit was 255. The problem query (Query1) is based on...
4
2703
by: Don Wash | last post by:
Hi All! I'm getting the following Error: No DLLs has been compiled yet and nothing in the \bin directory. So it is not the versioning problem or anything like that. And here are the...
12
6069
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
6
7825
by: Rolf Welskes | last post by:
Hello, if I have for example: <table style="width: 100%; height: 100%;" border="1"> <tr> <td style="width: 100px">k </td> <td style="width: 100px">k </td> </tr>
3
2149
by: key9 | last post by:
Hi all I am confuse of how to override operate "<<" Sample , I've got 2 class class Terminal { public:
8
1771
by: Prateek | last post by:
Hey... Can anybody tell me why "<<" operator is used behind cout? Like: cout<<"Welcome"; My one friend told me that it "shifts the bits"...But it's not very clear to me...Can anybody tell?
11
2216
by: Holger | last post by:
Hi I have not been able to figure out how to do compound statement from C - "<test>?<true-val>:<false-val>" But something similar must exist...?! I would like to do the equivalent if python...
10
2817
by: herbu | last post by:
In order to automatically check if the index of an array declared by vector<T> exceeding its boundary, we define the class "Vector" derived from "vector" and do the operator overloading as shown in...
4
8513
rahuljaiswal1987
by: rahuljaiswal1987 | last post by:
var path = Components.classes.getService(Components.interfaces.nsIProperties).get('ProfD', Components.interfaces.nsIFile).path; var localFile =...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.