英文:
Too many arguments in function call. c++
问题
我的目标是将这个向量 vector 'Beer' allBeers 传递给函数 UnitedStatesBeer::getBeerTop(),但当我尝试这样做时,我收到错误消息,函数调用中有太多参数,或者我的对象未初始化。
我该如何修复这个问题?感谢你的帮助!
英文:
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
}
答案1
得分: 1
首先,由于您想要覆盖虚函数,您需要将基类中的函数定义与 RatingCalculator
中的函数定义匹配。在 RatingCalculator
中如下所示:
virtual void getCountryTop(vector<Beer>& allBeers) {};
而在 UnitedStatesBeer
中应该是:
void getCountryTop(vector<Beer>& allBeers) {};
其次,由于您想要获取前几款啤酒,您可能希望通过引用传递,可以在变量 beers
前添加 &
。
第三,
UsReassign->getCountryTop(allBeers); // <- expression (UsReassign) must have pointer type/ Using uninitialized memory..
出现这种情况是因为 UsReassign
是一个对象,而不是一个指针。对象使用 .
而不是 ->
进行引用。
最后,
// UsReassign-> getCountryTop(allBeers); // <- too many arguments in function call
这是因为正在调用没有参数的函数 getCountryTop()
。在“首先”中提到的步骤应该会修复这个问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论