Too many arguments in function call. c++

huangapple go评论97阅读模式
英文:

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&lt;Beer&gt; allBeers) {};

and in UnitedStatesBeer

 void getCountryTop(vector&lt;Beer&gt; allBeers){};

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

Thirdly,
UsReassign-&gt;getCountryTop(allBeers); //&lt;- 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 -&gt;.

And Lastly,
// UsReassign-&gt; getCountryTop(allBeers); //&lt;- 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.

huangapple
  • 本文由 发表于 2020年1月3日 23:19:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定