Why compiler complants error when compile.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 3
Reputation: banbangou is an unknown quantity at this point 
Solved Threads: 0
banbangou banbangou is offline Offline
Newbie Poster

Why compiler complants error when compile.

 
0
  #1
Mar 17th, 2005
Anybody could tell me what's wrong with the following code?
It complaints:
13 C:\apps\Dev-Cpp\projects\14\14_1.cpp ISO C++ forbids defining types within return type

Thanks in advance.


  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. struct vector {
  7. double x;
  8. double y;
  9.  
  10. friend ostream& operator<< (ostream&, vector);
  11. }
  12.  
  13. ostream& operator<< (ostream& o, vector a) {
  14. o << "(" << a.x << "," << a.y << ")" << endl;
  15. return o;
  16. }
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20. vector v1;
  21. v1.x = 1;
  22. v1.y = 1;
  23.  
  24. cout << v1 << endl;
  25.  
  26. system("PAUSE");
  27. return 0;
  28. }

<< moderator edit: added [code][/code] tags >>
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why compiler complants error when compile.

 
0
  #2
Mar 17th, 2005
There will be ambiguity with your class and std::vector since you are using namespace std.
struct vector
{
   double x;
   double y;

   friend ostream& operator<< (ostream&, vector);
};
If you still want to grab the whole namespace, I think you can disambiguate like this.
friend ostream& operator<< (ostream&, ::vector);
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Why compiler complants error when compile.

 
0
  #3
Mar 20th, 2005
or in full "std::vector", but there is still a danger that the struct name will cause errors. Easy just to do

  1.  
  2. namespace my_vector
  3. {
  4. struct vector // might as well class it
  5. {
  6. double x, y;
  7.  
  8. friend ostream& operator<< (ostream& out, const vector& v) // I use references but its not compulsory
  9. {
  10. out << "(" << a.x << "," << a.y << ")" << endl;
  11. return out;
  12. }
  13. };
  14. }

then in the main just use my_vector::vector v1; instead. This solves any name problems you might have by making your own namespace
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1
Reputation: quickhelp is an unknown quantity at this point 
Solved Threads: 0
quickhelp quickhelp is offline Offline
Newbie Poster

Re: Why compiler complants error when compile.

 
0
  #4
Apr 10th, 2005
From what I see you're missing a semicolon after the '}' from your definition of your struct.
hope this helps..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC