我对这个错误感到有些困惑(我正在使用VS2012)。
这是我的代码: RecipeBook.h:
#ifndef RECIPEBOOK_H
#define RECIPEBOOK_H
#include "SingleRecipe.h"
using namespace std;
class RecipeBook
{
private:
vector<SingleRecipe> *recipe;
SingleRecipe *one;
public:
RecipeBook(vector<SingleRecipe> *recipe);
void addRecipe(SingleRecipe *one);
bool removeRecipe(string name);
vector <SingleRecipe> *returnListOfRecipes(double time);
};
#endif
SingleRecipe.h:
#ifndef SINGLERECIPE_H
#define SINGLERECIPE_H
#include <string>
#include <vector>
using namespace std;
class SingleRecipe
{
private:
string name;
vector<string> ingredients;
vector<string> method;
int numOfServing;
double time;
public:
SingleRecipe(string name, vector<string> ingredients, vector<string> method, int numOfServing, double time);
string getName();
void setName();
int getNumOfServing();
void setNumOfServing();
double getTime();
void setTime();
string toString();
};
#endif
BookAndRecipe.cpp:
#include "RecipeBook.h"
#include "SingleRecipe.h"
#include <sstream>
#include <math.h>
using namespace std;
vector <SingleRecipe> *RecipeBook::returnListOfRecipes(double time)
{
vector<SingleRecipe> *two;
for (int i = 0; i = recipe->size(); i++)
{
if (recipe[i].data()->getTime < time)
{
*two->push_back(recipe[i].pop_back());
}
}
return NULL;
}
在returnListOfRecipes()结束时出现此错误:
no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=SingleRecipe, _Alloc=std::allocator<SingleRecipe>]" matches the argument list
argument types are: (void)
object type is: std::vector<SingleRecipe, std::allocator<SingleRecipe>> c:\Users\Ventus\Documents\Visual Studio 2012\Projects\Recipe\Recipe\BookAndRecipe.cpp 83 8
我怀疑我的for循环可能有问题,但我不是很有经验,所以我可能在这里做错了。
我感谢所有帮助!
答案 0 :(得分:0)
pop_back()
不返回值,只删除容器的最后一个元素。你可能想要:
*two->push_back(recipe[i].back());
recipe[i].pop_back();