Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hello everyone. I got a problem with my source codes in c++. This is a problem

I got a class 'Vector2D'
class Vector2D
{
  public:
      double x;    double y;
      Vector2D():x(0.0), y(0.0)
      {}
      
      Vector(double val1, double val2):x(val1), y(val2)
      {}
}


When in another class i want to use default parameter with 'Vector2D' like this
C++
bool CanShoot(Vector2D& Target=Vector2D())
{......}


But i have an error: default argument for parameter of type 'Vector2D&' has type 'Vector2D'

How can i pass a reference parameter with a default parameter?
Posted
Updated 16-Sep-11 23:54pm
v2

According to standard, it is illegal to bind a non const reference to a temporary object. Visual Studio incorrectly allows it but give a warning at level 4.

Thus the function should be declared as:

C++
bool CanShoot(const Vector2D& Target=Vector2D()) { return true; } 


However, it would be preferable to add an overload in this case:

C++
bool CanShoot()
{
    Vector2D temp;
    return CanShoot(temp);
}


This has the advantage that the "default value" can be hidden in the implementation. And it can allows some optimisations to be done.
 
Share this answer
 
Comments
Philippe Mori 17-Sep-11 10:35am    
The following link give some information on the handling of references by Visual C++ 2010: Rvalue References.

I hope Visual C++ will fix this problem. Until then, I recommand anyone to add /we4239 option so that this warning will be handled as an error. It works at any warning level.
Stefan_Lang 19-Sep-11 6:15am    
Thanks for the link and tip. My 5!
I like Phillipe Moris solution better, but here is an alternate:

C++
class Vector2D {
   // ... your stuff goes here
};
const Vector2D null_vector; // implicitely calls default constructor

bool CanShoot(const Vector2D& target = null_vector) {
   // ... your code here
}
 
Share this answer
 
Comments
Philippe Mori 19-Sep-11 9:19am    
I do like that solution. The variable could also be made static. That solution would be better when there are multiple defaults depending on the function.
nguyenle.it 19-Sep-11 18:57pm    
It work like a charm, thank you so much. and thank you everyone for giving me the solution.
Cannot see any problem:
C++
#pragma once
#include <stdio.h>
#include <tchar.h>

class Vector2D
{
public:
  double x;    double y;
  Vector2D(double val1=0.0double val2=0.0):x(val1), y(val2)
  {}
};

bool CanShoot(Vector2D& Target=Vector2D())
{
  printf("x: %lf, y: %lf\r\n",Target.x,Target.y);
  return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
  CanShoot();
  _gettch();
  return 0;
}


Regards.
 
Share this answer
 
Comments
Philippe Mori 17-Sep-11 10:16am    
Code is illegal and should not compile. See my solution.
mbue 17-Sep-11 11:39am    
This isnt illegal its the same behaviour like yours but inline.
The Vector2D() isnt a ctor rather than a constructor on the stack. The compiler produces the same result like your example and the two different function calls too.
Regards.
Emilio Garavaglia 18-Sep-11 12:41pm    
Philippe is right: according to the standard, you cannot bind a temporary to a reference, but only to a const-reference.
It is a well-known "Microsoft specific behavior", that non-microsoft compiler developer simply call "bug" (working differently than specified).
mbue 18-Sep-11 13:25pm    
Should this be a joke? Everything on earth is temporarily. This sounds like "you can never use references except const"? Try to understand the temporary class on stack is fully accessible like other declarations, thats there is no reason to make a call to a const object. The objects lifetime is enshured during the function call.
If other compilers dont accept this construct - explain why (no links accepted)!
Regards.
Stefan_Lang 19-Sep-11 6:12am    
You don't understand. In C/C++ 'temporary' is a term reserved for variables that are not explicitely listed by name in your code. instead they are created implicitely (by your compiler) as needed. Their lifetime does not exceed the statement they're used in, so if you tried to initialize a reference with one, this reference would be immediately invalidated again.
In your example, Vector2D() is the constructor of the class Vector2D. You cannot call the constructor explicitly. Instead, the constructor is called automatically when you instantiate an object of that class. For example:
Vector2D *foo = new Vector2D();
or with the declaration
Vector2D bar;
A class constructor has some hidden parameters that the compiler provides and you cannot.
 
Share this answer
 
Comments
nguyenle.it 17-Sep-11 12:01pm    
Thanks all, but i want to add reference parameters to the function with the default value. How can i do with my occasion above?
Emilio Garavaglia 18-Sep-11 12:49pm    
This answer is misleading and claims a false concept.
Calling a constructor just creates a temporary object.
The problem is that a temporary object cannot be give to a non-const reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900