Constructor - able to take 0 or more arguments

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 154
Reputation: tones1986 is an unknown quantity at this point 
Solved Threads: 0
tones1986 tones1986 is offline Offline
Junior Poster

Constructor - able to take 0 or more arguments

 
0
  #1
Mar 17th, 2008
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:

  1. const Vector v1;
  2. const Vector v2(1.0, 2.0, 3.0);

And this in my vector.cpp file:

  1. Vector::Vector(double new_x, double new_y, double new_z)
  2. {
  3. x = 0;
  4. y = 0;
  5. z = 0;
  6. }

And then this in my vector.h file:

  1. ...
  2. ...
  3. public:
  4. //Vector();
  5. Vector(double, double, double);
  6.  
  7. };

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 15
Reputation: dophine is an unknown quantity at this point 
Solved Threads: 0
dophine dophine is offline Offline
Newbie Poster

Re: Constructor - able to take 0 or more arguments

 
0
  #2
Mar 17th, 2008
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)
Last edited by dophine; Mar 17th, 2008 at 1:12 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC