Re: C++ Help needed (Sorry about the cross-posting)

From: Philipp Brandl (brandl_at_irt.de)
Date: 11/30/04

  • Next message: Ioannis Vranos: "Re: C++ website"
    Date: Tue, 30 Nov 2004 09:27:58 +0100
    
    

    Jedispy wrote:
    > UPDATE:
    > O.k. after a little sweat, I think I have it figured out. However now I'm
    > getting a strange error.
    >
    > Error message:
    > error C2447: missing function header (old-style formal list?)
    >
    > I'm getting the error from a function declaration. My function code
    > follows:
    > double calc_letter(void);
                              ^^^^ remove semicolon
    > {
    > double letter_grade;
    >
    > switch(b.addgrade)
    > {
    > case "A+": gpa = 4.0; break;
    > case "A": gpa = 3.75; break;
    > case "A-": gpa = 3.5; break;
    > case "B+": gpa = 3.3; break;
    > case "B": gpa = 2.5; break;
    > case "B-": gpa = 2.0; break;
    > case "C+": gpa = 1.5; break;
    > case "C": gpa = 1.0; break;
    > case "C-": gpa = 0.9; break;
    > case "D+": gpa = 0.5; break;
    > case "D": gpa = 0.1; break;
    > case "D-": gpa = 0.05; break;
    > case "F": gpa = 0.0; break;
    > default: valid_entry = false; break;
    > }
    } <--
    >
    > The entirety of my code follows:
    *snipped*

    First you are declaring the prototype of the function calc_letter. But
    just afterwards you are giving the definition of it.
    Either you remove the semicolon or you seperate function declaration
    (which the first line is) and definition (the whole block without the
    semicolon). Usually function declarations (prototypes) go in a header file.
    The second problem is that you aren't returning anything in your
    function. You are just accessing (probably) private member variables
    which is probably not what the function is supposed to do.

    I think it should be something like:
    double letter_to_grade(string letter);
    Now you can switch on the letter string and return the corresponding
    grade value.

    Look at the definition:
    double letter_to_grade(string letter);
    {
         switch(letter)
         {
         case "A+": return 4.0;
         case "A": return 3.75;
         ... and so on
         }
    }

    If you don't understand the following never mind and just skip over it.
    It's also a good idea to specify the keyword const in your function
    declaration if you aren't changing any member variables.
    Also this function is not specific to any class instance of grade. So
    it's reasonable to declare it as static. This means the function is the
    same for all instances of class grade as it doesn't access any member
    variables of a certain instance.

    So the suggested function header would be as follows:

    static double letter_to_grade(string letter) const;

    Greets
         Phil


  • Next message: Ioannis Vranos: "Re: C++ website"