| | |
Why compiler complants error when compile.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 3
Reputation:
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.
C++ Syntax (Toggle Plain Text)
#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); "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
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
C++ Syntax (Toggle Plain Text)
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:
![]() |
Similar Threads
- Dev-C++ - [Build Error] [main.o] Error 1 (C++)
- compiler error when address of a pointer is passed (C)
- Urgent Help in Binary Search Trees.. (C++)
- Linking error in the compiler given by topcoder. Plz help me out guys. (C++)
- compile error-chained hash table c++ (C++)
Other Threads in the C++ Forum
- Previous Thread: Strings and For Loops
- Next Thread: Missing output from program
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






