Default value for function parameter

This is a discussion on Default value for function parameter within the C++ Programming forums, part of the General Programming Boards category; function declaration Code: void gui::addMenuButton(int x, int y, string name, int parent = -1){ prototype Code: void addMenuButton(int x, int ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    30

    Default value for function parameter

    function declaration
    Code:
    void gui::addMenuButton(int x, int y, string name, int parent = -1){
    prototype
    Code:
    void addMenuButton(int x, int y, string name, int parent = -1);
    And for some odd reason, I get the errors:

    Code:
    C:\Users\Justin\Desktop\Logic\gui.cpp|11|error: default argument given for parameter 4 of 'void gui::addMenuButton(int, int, std::string, int)'|
    C:\Users\Justin\Desktop\Logic\gui.h|23|error: after previous specification in 'void gui::addMenuButton(int, int, std::string, int)'|

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,614
    You should only specify the default argument in one place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I found it good practice to specify the default parameter in the prototype and comment it out in the body:

    Code:
    void gui::addMenuButton(int x, int y, string name, int parent /*= -1*/){
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redefinition of default parameter
    By 843 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2011, 02:07 AM
  2. Replies: 9
    Last Post: 06-18-2009, 04:58 AM
  3. Default parameter
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2007, 10:59 AM
  4. Help with default parameter value using ptr-to-functor
    By registering in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2004, 03:21 PM
  5. default parameter
    By laasunde in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2003, 09:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21