| | |
Why compiler complants error when compile.
![]() |
•
•
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 application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






