•
•
•
•
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
![]() |
•
•
Join Date: Mar 2005
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
<< moderator edit: added [code][/code] tags >>
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 >>
There will be ambiguity with your class and std::vector since you are using namespace std. If you still want to grab the whole namespace, I think you can disambiguate like this.
struct vector { double x; double y; friend ostream& operator<< (ostream&, vector); };
friend ostream& operator<< (ostream&, ::vector); or in full "std::vector", but there is still a danger that the struct name will cause errors. Easy just to do
then in the main just use my_vector::vector v1; instead. This solves any name problems you might have by making your own namespace
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:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Dev-C++ - [Build Error] [main.o] Error 1 (C++)
- Simple Programming Errors (Computer Science and Software Design)
- compile error-chained hash table c++ (C++)
- compiler (C++)
- Java (C++)
Other Threads in the C++ Forum
- Previous Thread: Strings and For Loops
- Next Thread: Missing output from program



Linear Mode