Functions with varying augments

This is a discussion on Functions with varying augments within the C++ Programming forums, part of the General Programming Boards category; Hey, I'm curious how I can make a function that can receive a different number of augments and not produce ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Functions with varying augments

    Hey, I'm curious how I can make a function that can receive a different number of augments and not produce an error.

    All of the augments in my function are coordinates, some times it needs to manage only 2 augments and some times it may need to manage more.

    My compiler gives me this error:
    "too few augments to function `int check_aligned(int,int,int,int,int)' at this point in file"

    Thanks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Most probably you'd need arrays.

    Code:
    int check_aligned(int* values, int how_many);
    Or preferably a vector:

    Code:
    int check_aligned(std::vector<int> values);

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Post some code, will be easier to understand what you want to acheive.

    From what I understand, the function prototype and the call to the function ( ie: passing of the arguments ) must match
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    EDIT: Sorry, I forgot about vectors!!
    I'm just trying to be a better person - My Name Is Earl

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    If you actually need a variable number of arguments and not arrays, then either look up the old macros in <cstdarg> (bad idea) or overload the function or use default arguments.
    Code:
    // Overloading
    void function(int x) {}
    void function(char c, float z) {}
    
    function(4);
    function('A', 1.2);
    
    // Default arguments
    void functionx(int x = 1, int y = 1);
    
    void functionx(int x, int y) {}
    
    functionx();
    functionx(1);
    functionx(1, 2);
    [edit] There are lots of tutorials that cover this, one is: http://www.functionx.com/cpp/Lesson05.htm [/edit]
    Last edited by dwks; 11-24-2006 at 03:36 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks anon, I understand the one with the arrays, but how do you use the vectors?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Yes, I like providing links to tutorials . . . http://www.cprogramming.com/tutorial/stl/vector.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Ha, I'm sorry, and also a little lazy. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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