1

I am new to C++ (from C# background) and I have a function with the following signature

 void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude = false, float scale_x =.15, float scale_y =15, float scale_z = 15, int positionx = 250, int positiony = 100, int positionz =0 )
{

}

But when I try to call the code as below, I get an error which says function does not take 4 arguments.

//then I try to call my function like so
AddBenchNode(root, benches, false, 250);

but I instead get the following error message

error C2660: 'AddBenchNode' : function does not take 3 arguments

Would appreciate an explanation of how C++ does this instead?

3
  • 8
    The default parameters should be declared in the header file, not in the implementation file. Commented Jul 5, 2012 at 14:15
  • 1
    You should provide a minimal self-contained compiling example everyone can take and try for themselves to see what you see. Commented Jul 5, 2012 at 14:15
  • Post full compilation error, please. Commented Jul 5, 2012 at 14:16

1 Answer 1

7

Check the prototype in your .hpp file. It's probably declared as

void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude, 
                  float scale_x, float scale_y, float scale_z, int positionx, 
                  int positiony, int positionz);

EDIT: The prototype in the header should be

void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude = false, float scale_x =.15, float scale_y =15, float scale_z = 15, int positionx = 250, int positiony = 100, int positionz =0 );

And your cpp file should then only have

void AddBenchNode(ref_ptr<Group> root ,ref_ptr<Node> benches, bool setAttitude, float scale_x, float scale_y, float scale_z, int positionx, int positiony, int positionz)
{

}

That is, the default parameters are in the prototype, not in the actual definition.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the prototype void AddBenchNode(osg::ref_ptr<osg::Group>,osg::ref_ptr<osg::Node>, bool, float, float, float, int, int, int );
My question is why I am not able to call the same function this way AddBenchNode(root, benches, false, 250);
+1, but I think your first line should say "declared" and the last line should say "definition". Just picking nits ;-)
Thank you. You are right, I had it the other way around. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.