简体   繁体   中英

Too many arguments in function call. c++

So my goal is to pass this vector vector 'Beer' allBeers to a function UnitedStatesBeer::getBeerTop() , but when I try to do that, I get the error that there's too many arguments in function call, or my object is not initialized.

How do I fix this? Thanks for your help!

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

class RatingCalculator
{
public:
     virtual void showCountryTop() = 0;
     virtual void getCountryTop() {};
};

class Beer
{
public:
    string name;
    string rating;
    string country;
    string alc;
    string type;
};

class UnitedStatesBeer : public RatingCalculator
{
private:
    string name;
    string rating;
    string country;
    string alc;
    string type;
public:
    void showCountryTop() {};
    void getCountryTop(vector<Beer> allBeers){};

int main()
{
        ifstream file("beer.txt");
        Beer currentBeer;
        vector<Beer> allBeers;

        for (int i = 0; !file.eof(); i++)
        {
            getline(file, currentBeer.name, '\t');
            getline(file, currentBeer.rating, '\t');
            getline(file, currentBeer.country, '\t');
            getline(file, currentBeer.alc, '\t');
            getline(file, currentBeer.type, '\n');

            allBeers.push_back(currentBeer);    //copy all the information to allBeers vector 
        }
        file.close();

        /*if I do it this way*/
        UnitedStatesBeer UsReassign;          
        UsReassign->getCountryTop(allBeers);   //<- expression (UsReassign) must have pointer type/ Using uninitialized memory

    //  RatingCalculator* UsReassign = new UnitedStatesBeer();
    //  UsReassign-> getCountryTop(allBeers);  //<- too many arguments in function call


    }

First off, Since you want to override virtual function, you'd want to match function definition in Base Class aka RatingCalculator with the one in UnitedStatesBeer . This is how it should look in RatingCalculator :

  virtual void getCountryTop(vector<Beer> allBeers) {};

and in UnitedStatesBeer

 void getCountryTop(vector<Beer> allBeers){};

Secondly, since you want to get Top beers, you may want to pass by reference, by adding & in front of variable beers .

Thirdly, UsReassign->getCountryTop(allBeers); //<- expression (UsReassign) must have pointer type/ Using uninitialized memory.. UsReassign->getCountryTop(allBeers); //<- expression (UsReassign) must have pointer type/ Using uninitialized memory.. This is happening because UsReassign is a object and NOT a pointer. Objects are referenced with . instead of -> .

And Lastly, // UsReassign-> getCountryTop(allBeers); //<- too many arguments in function call // UsReassign-> getCountryTop(allBeers); //<- too many arguments in function call This is happening because function getCountryTop() with NO arguments is being called. Steps mentioned in "Firstly" should fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM