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
    22,268
    You should only specify the default argument in one place.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 03:07 AM
  2. Replies: 9
    Last Post: 06-18-2009, 05:58 AM
  3. Default parameter
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2007, 11: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, 04:21 PM
  5. default parameter
    By laasunde in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2003, 10:55 PM

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