| | |
Constructor - able to take 0 or more arguments
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 154
Reputation:
Solved Threads: 0
Hey folks. I am confused as to how i can make a constructor take zero - or lets say 3 arguments. If i do this in my main.cpp:
And this in my vector.cpp file:
And then this in my vector.h file:
I get an error saying this:
23 C:\Users\Tony\Documents\csci 241\Assign4\assign4.cpp no matching function for call to `Vector::Vector()'
THis makes sense as there is no constructor like this within my .h or .cpp file - so it doesnt know what to do. But i MUST have only one constructor, is this possible?
Hope you guys can help out again
Thanks in advance
C++ Syntax (Toggle Plain Text)
const Vector v1; const Vector v2(1.0, 2.0, 3.0);
And this in my vector.cpp file:
C++ Syntax (Toggle Plain Text)
Vector::Vector(double new_x, double new_y, double new_z) { x = 0; y = 0; z = 0; }
And then this in my vector.h file:
C++ Syntax (Toggle Plain Text)
... ... public: //Vector(); Vector(double, double, double); };
I get an error saying this:
23 C:\Users\Tony\Documents\csci 241\Assign4\assign4.cpp no matching function for call to `Vector::Vector()'
THis makes sense as there is no constructor like this within my .h or .cpp file - so it doesnt know what to do. But i MUST have only one constructor, is this possible?
Hope you guys can help out again
Thanks in advance
•
•
Join Date: Feb 2008
Posts: 15
Reputation:
Solved Threads: 0
as you provide a constructor, the compiler does not generate a default constructor which takes no parameter for you.
so if you want a constructor to take zero parameter, then you must define it.
or you can try this to provide default value
Vector::Vector(double new_x=2.0, double new_y=2.0, double new_z=2.0)
so if you want a constructor to take zero parameter, then you must define it.
or you can try this to provide default value
Vector::Vector(double new_x=2.0, double new_y=2.0, double new_z=2.0)
Last edited by dophine; Mar 17th, 2008 at 1:12 pm.
![]() |
Similar Threads
- constructor arguments with new (C++)
- Objects are not displaying in my program, also need to know how to work mouse clicks. (C++)
- Constructor ? (Java)
- Java.io help!!! (Java)
- C++ help with classes/constructors (C++)
- Accessing a variable of another class (Java)
- Need help Using "get and set methods" (Java)
Other Threads in the C++ Forum
- Previous Thread: Working with doubles, having some troubles
- Next Thread: template vs inheritance
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





