0

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


    }
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
tendor
  • 73
  • 1
  • 8
  • Please create a [mcve] to show us, something which we can copy-paste and try ourselves without needing to edit or modify it in any way. Also please copy-paste the full and complete error output into the question (as text), as often there could be helpful informational notes that could give hints to the problem. – Some programmer dude Jan 03 '20 at 15:22
  • 3
    A hint though: To override a virtual function, the function signature (like e.g. arguments) must be *exactly* the same as in the base class. – Some programmer dude Jan 03 '20 at 15:23
  • 1
    [Why is `iostream::eof` inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – molbdnilo Jan 03 '20 at 15:29
  • Why does `UnitedStatesBeer` have a `country` property? I would expect to see `class UnitedStatesBeer : public Beer`, since beer from the US is a kind of beer (well, at least some of it is) and not a kind of calculator. – molbdnilo Jan 03 '20 at 15:33
  • 1
    Note that you are not overriding your virtual correctly. `RatingCalculator::getCountryTop()` vs. `UnitedStatesBeer::getCountryTop(vector allBeers)`. – Fred Larson Jan 03 '20 at 15:34
  • I'm missing the closing brace and semicolon `};` of `class UnitedStatesBeer`. As such, you are declaring `main()` as a member function of `UnitedStatesBeer`. Probably not what you wanted to do... – cmaster - reinstate monica Jan 03 '20 at 17:35
  • IMHO, you should change your design. Use inheritance for *is-a* relationships and composition for *has-a* relationships. A `UnitedStatesBeer` is not a calculator, it is a kind of beer; so `UnitedStatesBeer` should inherit from `Beer`. Look at the the two classes, they have a lot in common. Maybe the calculator should be a separate class that *contains* different beers. – Thomas Matthews Jan 03 '20 at 19:36

1 Answers1

1

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.. 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 This is happening because function getCountryTop() with NO arguments is being called. Steps mentioned in "Firstly" should fix it.

E.N.D
  • 1,151
  • 5
  • 10