Thread: Function Help

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    27

    Function Help

    I'm trying to use the following in a program, but seem to be getting an error. Any ideas why?

    Code:
    bool greater (int a, int b)
    {
         if (a > b)
            return true;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Functions need to return a value. Currently your function only returns a value when the test is true. You still need to return a false value if the test is false. Change it to this:

    Code:
    bool greater(int a, int b)
    {
        return (a > b);
    }
    Last edited by hk_mp5kpdw; 03-01-2006 at 02:23 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    My compilier says there's a linker error...

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    There is a problem with the function which I've noted and corrected above.

    A nit... compilers show compiler errors, linkers show linker errors.

    Don't know what could be causing the linker error... there is a templated function object called greater defined in the <functional> header but I don't think that would cause a problem (maybe?).

    Maybe you could show some more of your code if it isn't too big.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    I'm sorry, I didn't see the re-written code. Thanks!

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by boxsterh
    I'm sorry, I didn't see the re-written code. Thanks!
    I had edited my post while you were making your post... it was a timing issue.

    As to the error, just considering your original function's code, the compiler (not linker) should have generated an error (or is it a warning?) that said something along the lines of "not all control paths return a value".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM