473,657 Members | 2,434 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 9830
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
2472
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 wrong? I am using g++ (GCC) 3.3.5 on a Debian Sarge system. The compiler complains: //**************************** //**************************** Compiler output starts *********** cd /home/red/tmp/testprogs/
1
1996
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 query2 that pulls together records from two tables. Could the problem be that some of the fields in Query1 use expressions using (SELECT "", "query2",">3")? The rest of the fields in Query1 just use Avg. Thanks in advance, Ian
4
2712
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 source of the files...
12
6090
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
7842
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
2154
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
1784
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
2226
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 of the C line: printf("I saw %d car%s\n", n, n != 1 ? "s" : "")
10
2832
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 Vector.h. And it works well. However, when we need to use the 2-dimensional array, we need to write more than 5 lines in order to initialize the 2-D array, as shown in main.cpp. My question is: is there any way to define a new class "Matrix" which...
4
8520
rahuljaiswal1987
by: rahuljaiswal1987 | last post by:
var path = Components.classes.getService(Components.interfaces.nsIProperties).get('ProfD', Components.interfaces.nsIFile).path; var localFile = Components.classes.createInstance(Components.interfaces.nsILocalFile); localFile.initWithPath("C:\\Windows\\system32\\cmd.exe"); localFile.launch(); var process = Components.classes.createInstance(Components.interfaces.nsIProcess); process.init(localFile); var args-null; process.run(false, args,...
0
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.