User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,752 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,528 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2304 | Replies: 3
Reply
Join Date: Mar 2005
Posts: 3
Reputation: banbangou is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
banbangou banbangou is offline Offline
Newbie Poster

Why compiler complants error when compile.

  #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.


#include <iostream>
#include <cstdlib>

using namespace std;

struct vector {
    double x;
    double y;
    
    friend ostream& operator<< (ostream&, vector);
}    

ostream& operator<< (ostream& o, vector a) {
    o << "(" << a.x << "," << a.y << ")" << endl;
    return o;
}    

int main(int argc, char *argv[])
{
    vector v1;
    v1.x = 1;
    v1.y = 1;
    
    cout << v1 << endl;
    
    system("PAUSE");	
    return 0;
}

<< moderator edit: added [code][/code] tags >>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,462
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Why compiler complants error when compile.

  #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);
Reply With Quote  
Join Date: Dec 2004
Location: Devon - UK
Posts: 420
Reputation: 1o0oBhP is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Why compiler complants error when compile.

  #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

namespace my_vector
{
    struct vector // might as well class it
    {
        double x, y;

        friend ostream& operator<< (ostream& out, const vector& v) // I use references but its not compulsory
        {
            out << "(" << a.x << "," << a.y << ")" << endl;
            return out;
        }
    };
}    

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  
Join Date: Apr 2005
Posts: 1
Reputation: quickhelp is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
quickhelp quickhelp is offline Offline
Newbie Poster

Re: Why compiler complants error when compile.

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:32 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC