函数调用中的参数太多。C++
所以我的目标是将这个向量'Beer'allBeers传递给一个函数UnitedStatesBeer::getBeerTop,但是当我尝试这样做时,我得到的错误是函数调用中的参数太多,或者我的对象没有初始化 我该如何解决这个问题?谢谢你的帮助函数调用中的参数太多。C++,c++,function,class,object,vector,C++,Function,Class,Object,Vector,所以我的目标是将这个向量'Beer'allBeers传递给一个函数UnitedStatesBeer::getBeerTop,但是当我尝试这样做时,我得到的错误是函数调用中的参数太多,或者我的对象没有初始化 我该如何解决这个问题?谢谢你的帮助 #include <iostream> #include <fstream> #include <string> #include <iomanip> #include <vector> usin
#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
}
首先,由于要覆盖虚拟函数,所以需要将基类(也称为RatingCalculator)中的函数定义和UnitedStatesBeer中的函数定义相匹配。在RatingCalculator中应该是这样的: 第二,因为你想得到顶级啤酒,你可能想通过引用,在变量啤酒前面添加&来传递 第三,, UsReassign->getCountryTopallBeers;// 最后,
//UsReassign->getCountryTopallBeers//请创建一个来向我们展示,我们可以复制粘贴并自己尝试,而无需以任何方式编辑或修改它。另外,请将完整的错误输出以文本形式复制粘贴到问题中,因为通常会有一些有用的信息注释,可以为问题提供提示,函数签名(如参数)必须与基类中的完全相同。为什么UnitedStatesBeer具有country属性?我希望看到UnitedStates类啤酒:公共啤酒,因为来自美国的啤酒是一种啤酒,至少有一部分是,而不是一种计算器。请注意,您没有正确覆盖虚拟啤酒。RatingCalculator::getCountryTop vs.UnitedStates Beer::getCountryTopvector allBeers。在编写RatingCalculator::getCountryTopvector allBeers后,我遇到一个错误:C2065:“Beer”:未声明的标识符和C2923:“std::vector”:“Beer”不是参数“\u Ty”@t的有效模板类型参数,或者尝试将Beer类放在RatingCalculator之前
virtual void getCountryTop(vector<Beer> allBeers) {};
void getCountryTop(vector<Beer> allBeers){};