943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3152
  • C++ RSS
Mar 17th, 2005
0

Why compiler complants error when compile.

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  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 >>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
banbangou is offline Offline
3 posts
since Mar 2005
Mar 17th, 2005
0

Re: Why compiler complants error when compile.

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);
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 20th, 2005
0

Re: Why compiler complants error when compile.

or in full "std::vector", but there is still a danger that the struct name will cause errors. Easy just to do

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Apr 10th, 2005
0

Re: Why compiler complants error when compile.

From what I see you're missing a semicolon after the '}' from your definition of your struct.
hope this helps..
Reputation Points: 10
Solved Threads: 0
Newbie Poster
quickhelp is offline Offline
1 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Strings and For Loops
Next Thread in C++ Forum Timeline: Missing output from program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC