Thread: "Prototype does not match any in class" error

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    110

    "Prototype does not match any in class" error

    Code:
    #include <iostream>using namespace std;
    
    
    class Rectangle {
        int breadth = 0;
        int length = 0;
        public:
        int area (int, int);
        int perimeter (int, int);
        void SetValues (int, int);
    };
    
    
    Rectangle::area (int l, int h) {
        l = breadth;
        h = length;
        return l * h;
    }
    
    
    Rectangle::SetValues (int First_Value, int Second_Value) {
        breadth = First_Value;
        length = Second_Value;
    }
    
    
    int main () {
    // More code to be inserted at a later stage
        return 0;
    }
    Line 21 gives the error message: "Prototype does not match any in class" error.
    Line 10 also gives the error "candidate is void Rectangle::SetValues(int, int)

    What I notice is that by changing line 10 to int SetValues (int, int); resolves the error. But why?

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by Vespasian_2 View Post

    What I notice is that by changing line 10 to int SetValues (int, int); resolves the error. But why?
    The return types you use when prototyping functions have to match those you use when defining the functions.

    Both your function definitions don't have an explicit return type. The syntax is:

    Code:
    void Rectangle::SetValues(int First_value, int Second_value)
    {
     //code
    }

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Aslaville View Post
    The return types you use when prototyping functions have to match those you use when defining the functions.

    Both your function definitions don't have an explicit return type. The syntax is:

    Code:
    void Rectangle::SetValues(int First_value, int Second_value)
    {
     //code
    }
    Thanks, it works now.

    On a related note, why does the following code print out an area of 0 instead of 25?



    Code:
    #include <iostream>using namespace std;
    
    
    class Rectangle {
        public:
        int length = 0;
        int breadth = 0;
        int calculate_area ();
        int calculate_perimeter (int, int);
        void SetValues (int, int);
        int area = calculate_area();
    };
    
    
    int Rectangle::calculate_area () {
    
    
        return length * breadth;
    }
    
    
    void Rectangle::SetValues (int First_Value, int Second_Value) {
        length = First_Value;
        breadth = Second_Value;
    }
    
    
    int main () {
        Rectangle Rectangle1;
        Rectangle1.SetValues(5, 5);
        cout << Rectangle1.area;
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You didn't call .area as a function. You're likely printing some default representation of the address of the function itself.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Elkvis View Post
    You didn't call .area as a function. You're likely printing some default representation of the address of the function itself.
    Which line number?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Line 31.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Elkvis View Post
    Line 31.
    But why is that an error? I mean I can call another object Rectangle1 variable, say, length or breadth which doesnt return an area.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Line 11: int area = calculate_area();
    This declares a variable area which is initialized when the object is created to the value returned by calculate_area. Since both those are initialized to 0, the result is 0. C++ does not have any kind of declarations where you establish some kind of relationship. The variable area is never recalculated when some variables change. It is initialized first (and only) when the line of code is executed, and that is when the object is first created. Make area a function or call calculate_area.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Elysia View Post
    Line 11: int area = calculate_area();
    This declares a variable area which is initialized when the object is created to the value returned by calculate_area. Since both those are initialized to 0, the result is 0. C++ does not have any kind of declarations where you establish some kind of relationship. The variable area is never recalculated when some variables change. It is initialized first (and only) when the line of code is executed, and that is when the object is first created. Make area a function or call calculate_area.
    Okay so if I can confirm what that means is that the value "area" really cannot change during the life of the programme?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The way you've programmed it, it will not change during the life of the programme.

    The statement:
    int area = calculate_area();
    is run once and only once during the whole life of the programme.

    I suggest you remove it and call calculate_area() instead:
    cout << Rectangle1.calculate_area();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Elysia View Post
    The way you've programmed it, it will not change during the life of the programme.

    The statement:
    int area = calculate_area();
    is run once and only once during the whole life of the programme.

    I suggest you remove it and call calculate_area() instead:
    cout << Rectangle1.calculate_area();
    That works.

    Even better I set the values within calculate area as follows:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
     class Rectangle {
        public:
        int length = 0;
        int breadth = 0;
        int calculate_area (int, int);
        int calculate_perimeter (int, int);
        void SetValues (int, int);
    //    int area = calculate_area();
    };
    
    
    int Rectangle::calculate_area (int First_Value, int Second_Value) {
        length = First_Value;
        breadth = Second_Value;
        return length * breadth;
    }
    
    
    void Rectangle::SetValues (int First_Value, int Second_Value) {
        length = First_Value;
        breadth = Second_Value;
    }
    
    
    
    
    int main () {
        Rectangle Rectangle1;
        //Rectangle1.SetValues(5, 5);
        cout << Rectangle1.calculate_area(4, 5);
        return 0;
    }

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Now calculate_area() is not true to its name. I don't expect calculate_area() to actually store new breath & length if I were to call it, and yet that's what you do. Calculate_area() has one function: to calculate the area and that's it. It should not attempt to do more. Preferably it should use the class's length and breadth members; otherwise there's no point of it being a member function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Elysia View Post
    Now calculate_area() is not true to its name. I don't expect calculate_area() to actually store new breath & length if I were to call it, and yet that's what you do. Calculate_area() has one function: to calculate the area and that's it. It should not attempt to do more. Preferably it should use the class's length and breadth members; otherwise there's no point of it being a member function.
    Okay, I see what you mean. The function is doing more things than what I described it.

    How about this then:
    Code:
    #include <iostream>
    
    using namespace std;
    
    
     class Rectangle {
        public:
        int length = 0;
        int breadth = 0;
        int calculate_area ();
        int calculate_perimeter (int, int);
        void SetValues (int, int);
    //    int area = calculate_area();
    };
    
    
    int Rectangle::calculate_area () {
        return length * breadth;
    }
    
    
    int main () {
        Rectangle Rectangle1;
        Rectangle1.length = 1;
        Rectangle1.breadth = 6;
        cout << Rectangle1.calculate_area();
    
    
    
        return 0;
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is much better. Note that calculate_area() also should be const.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Mar 2016
    Posts
    110
    Quote Originally Posted by Elysia View Post
    This is much better. Note that calculate_area() also should be const.
    I really appreciate your input, giving good advice over and above the question(s) asked.

    When you say const, you mean for the member function to return a const int

    Code:
    #include <iostream>
    
    using namespace std;
    
    
     class Rectangle {
        public:
        int length = 0;
        int breadth = 0;
        const int calculate_area ();
        int calculate_perimeter (int, int);
        void SetValues (int, int);
    //    int area = calculate_area();
    };
    
    
    const int Rectangle::calculate_area () {
        return length * breadth;
    }
    
    
    int main () {
        Rectangle Rectangle1;
        Rectangle1.length = 1;
        Rectangle1.breadth = 6;
        cout << Rectangle1.calculate_area();
        //Rectangle1.SetValues(5, 5);
        //cout << Rectangle1.calculate_area(4, 5);
        //cout << Rectangle1.calculate_area(4, 5);
           // Rectangle1.length = 1;
    
    
                //cout << Rectangle1.calculate_area(4, 5);
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Match-3 "Candy Crush" game using Arrays
    By Amanda Phelps in forum C Programming
    Replies: 2
    Last Post: 03-11-2015, 03:25 PM
  2. Replies: 4
    Last Post: 10-07-2012, 10:59 PM
  3. the operator "="error in class "String"
    By boyhailong in forum C++ Programming
    Replies: 9
    Last Post: 07-27-2011, 08:39 PM
  4. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  5. Replies: 9
    Last Post: 03-30-2009, 06:37 PM