Thread: Prototype for '' does not match any in class error

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    5

    Question Prototype for '' does not match any in class error

    Hi,

    New to c++ and have an error that I cannot resolve. Thinking I've just made a very basic mistake and cannot see it.

    The error is "prototype for 'void Menu::newItem(std::string, std::string, int)' does not match any in class 'Menu'"

    I am certain they match, all similar stack overflow questions were trying to pass too many variables or had different return types which is why this seems so strange.

    menu.h
    Code:
    #ifndef MENU_H
    #define MENU_H
    
    
    #include <string>
    using std::string;
    #include <iostream>
    #include "item.h"
    //#define MAXLEN 20
    
    
    class Menu {
    private:
            string _fName;
            string _mName;
            Item _db[20]; 
            int _nElems;
    public:
            Menu(string mName) : _fName(""), _mName(mName), _nElems(0) {  };
           
            void newItem(string category, string description, int price);
            
            void display();
            
    };
    #endif
    menu.cpp
    Code:
    #include "menu.h"
    #include <string>
    using std::string;
    #include <iostream>
    using std::cout;
    using std::endl;
    
    
    
    
    void Menu::newItem(string category, string description, int price){
            _db[_nElems] = Item(category, description, price);
            _nElems++;
    }
    
    
    void Menu::display(){
            string div = 
            "+--+-------+---------------------------------+-----+\n";
            cout << div <<
            "|No|Cat.   |Description                      |Price|\n" << div;
            for(int i = 0; i < _nElems; i++) {
                    _db[i].display(i);
            }
            cout << div;
    }
    Any help would be greatly appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're right: they match.

    It may make no difference, but I suggest that you remove the using declaration from the header file: fully qualify std::string there instead. In the source file, move the using declaration to after the last header inclusion. The reason is that using declarations at file scope in a header can change the meaning of code in the files that include the header, and using declarations before a header can change the meaning of code in the included header.

    Other things to tell us: which line does the error reference? What's in item.h?
    Last edited by laserlight; 04-07-2021 at 04:42 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    5
    Unfortunately your suggestions made no difference.

    Quote Originally Posted by laserlight View Post
    Other things to tell us: which line does the error reference? What's in item.h?
    in menu.cpp:
    Code:
     void Menu::newItem(std::string category, std::string description, int price){
    and in menu.h ([ERROR] candidate is: void Menu::newItem()):
    Code:
             void newItem(std::string category, std::string description, int price);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which compiler/IDE are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    5
    Quote Originally Posted by Salem View Post
    Which compiler/IDE are you using?
    Dev C++

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you talking about the archaic "Bloodshed" Dev-C++ or the somewhat up to date "Orwell" Dev-C++.

    Because the bloodshed one is about 20 years out of date.

    Dev-C++ download | SourceForge.net
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2021
    Posts
    5
    I'm embarrassed to say the bloodshed one. It has been pushed quite heavily on my course. Will see if the code compiles on another IDE now.

    EDIT: I'm actually using the version linked above.
    Last edited by ScottChegg; 04-07-2021 at 06:22 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're using the Orwell one and it doesn't work?

    I commented out Item (not that it should matter for the purpose of the exercise), and it compiles just fine here

    Code:
    $ cat menu.h
    #ifndef MENU_H
    #define MENU_H
     
     
    #include <string>
    using std::string;
    #include <iostream>
    //#include "item.h"
    //#define MAXLEN 20
     
     
    class Menu {
    private:
            string _fName;
            string _mName;
            //Item _db[20]; 
            int _nElems;
    public:
            Menu(string mName) : _fName(""), _mName(mName), _nElems(0) {  };
            
            void newItem(string category, string description, int price);
             
            void display();
             
    };
    #endif
    $ cat menu.cpp
    #include "menu.h"
    #include <string>
    using std::string;
    #include <iostream>
    using std::cout;
    using std::endl;
     
     
     
     
    void Menu::newItem(string category, string description, int price){
            //_db[_nElems] = Item(category, description, price);
            _nElems++;
    }
     
     
    void Menu::display(){
            string div = 
            "+--+-------+---------------------------------+-----+\n";
            cout << div <<
            "|No|Cat.   |Description                      |Price|\n" << div;
            for(int i = 0; i < _nElems; i++) {
                    //_db[i].display(i);
            }
            cout << div;
    }
    $ g++ -c menu.cpp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Apr 2021
    Posts
    5
    Quote Originally Posted by Salem View Post
    You're using the Orwell one and it doesn't work?

    I commented out Item (not that it should matter for the purpose of the exercise), and it compiles just fine here
    Yes, definitely using Orwell version 5.11.

    I also managed to get the code to compile today by moving the menu constructor out of the header file. Not sure how this error occurred and I'm not sure why it has gone away.

    If I had to guess I'd say the error occurred as I updated the newItem function and the compiler still expected to see the old parameters? Still, I don't think this explains why it disappeared today once I moved the constructor.

    Regardless thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-15-2019, 12:57 PM
  2. "Prototype does not match any in class" error
    By Vespasian_2 in forum C++ Programming
    Replies: 19
    Last Post: 04-05-2016, 10:54 AM
  3. class template Prototype?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2004, 03:37 PM
  4. virtual prototype in a class
    By sphreak in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 02:34 PM
  5. Is there anything wrong with this class prototype
    By Silvercord in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2003, 08:25 PM

Tags for this Thread