Help with vectors and class vectors

Hello there, I'm having a strange issue with some class vectors, and I have tried different ways and means to solve it without it affecting my current code, to no avail.

(I know the rules state that I'm not supposed to post homework, but this one is an optional thing put up by my teacher, and I would like to know how it works)

SingleRecipe.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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 


RecipeBook.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef RECIPEBOOK_H
#define RECIPEBOOK_H
#include "SingleRecipe.h"
using namespace std;

class RecipeBook
{
private:
	vector<SingleRecipe*> recipe;
public:
	RecipeBook(vector<SingleRecipe*> recipe);
	void addRecipe(SingleRecipe *one);
	bool removeRecipe(string name);
	vector<SingleRecipe*> returnListOfRecipes(double time);
};
#endif 


BookAndRecipe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "RecipeBook.h"
#include "SingleRecipe.h"
#include <sstream>
#include <math.h>
using namespace std;

SingleRecipe::SingleRecipe(string name, vector<string> ingredients, vector<string> method, int numOfServing, double time)
{
	this->name = name;
	this->ingredients = ingredients;
	this->method = method;
	this->numOfServing = numOfServing;
	this->time = time;
}
string SingleRecipe::getName()
{
	return name;
}
void SingleRecipe::setName()
{
	this->name = name;
}
int SingleRecipe::getNumOfServing()
{
	return numOfServing;
}
void SingleRecipe::setNumOfServing()
{
	this->numOfServing = numOfServing;
}
double SingleRecipe::getTime()
{
	return time;
}
void SingleRecipe::setTime()
{
	this->time = time;
}
string SingleRecipe::toString()
{
	stringstream info;
	info << "Recipe name: " << name << endl;
	info << "Recipe ingredients: " << endl;
	for (int i = 0; i < (signed int)ingredients.size(); i++)
	{
		info << ingredients[i] << endl;
	}
	info << "Recipe method: " << endl;
	for (int i = 0; i < (signed int)method.size(); i++)
	{
		info << method[i] << endl;
	}
	info << "Serving size: " << numOfServing << endl;
	info << "Time needed: " << floor(time) + 0.5 << endl;
	return info.str();
}
RecipeBook::RecipeBook(vector<SingleRecipe*> recipe)
{
	this->recipe = recipe;
}
void RecipeBook::addRecipe(SingleRecipe *one)
{
	recipe.push_back(one);
	this->recipe = recipe;
}
bool RecipeBook::removeRecipe(string name)
{
	for (int i = 0; i = recipe.size(); i++)
	{
		if (recipe[i]->getName() == name)
		{
			recipe.erase(recipe.begin() + i);
			return true;
		}
		else
		{
			break;
		}
	}
	return false;
}
vector<SingleRecipe*> RecipeBook::returnListOfRecipes(double time)
{
	vector<SingleRecipe*> test;
	for (int i = 0; i = recipe.size(); i++)
	{
		if (recipe[i]->getTime() < time)
		{
			test.push_back(recipe[i]);
			recipe.erase(recipe.begin() + i);
		}
	}
	return test;
}


Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "SingleRecipe.h"
#include "RecipeBook.h"
#include <iostream>
using namespace std;

int main()
{
	int input = 0;
	vector<SingleRecipe*> meow;
	RecipeBook three(meow);
	while (input != 5)
	{
		cout << "Main menu" << endl;
		cout << "1. Add recipe" << endl;
		cout << "2. Remove a recipe" << endl;
		cout << "3. Print all recipes which have preparation time below a certain time" << endl;
		cout << "4. Quit" << endl;
		cout << "Select option: ";
		cin >> input;
		if (input == 1)
		{
			cout << "Please enter the recipe details: " << endl;
			string name_input, end_input, endend_input;
			vector<string> ingredient_input, method_input;
			int servings;
			double timeRequired;
			cout << "Recipe name: ";
			getline(cin, name_input);
			cin.ignore();
			while(end_input != "end")
			{
				cin.clear();
				cin.sync();
				cout << "Please enter ingredient #" << ingredient_input.size() + 1 << "(type 'end' to end): ";
				getline(cin, end_input);
				ingredient_input.push_back(end_input);
			}
			ingredient_input.pop_back();
			while(endend_input != "end")
			{
				cin.clear();
				cin.sync();
				cout << "Please enter method #" << method_input.size() + 1 << "(type 'end' to end): ";
				getline(cin, endend_input);
				method_input.push_back(endend_input);
			}
			method_input.pop_back();
			cout << "How many servings does this serve? ";
			cin >> servings;
			cout << "How much time does it require? ";
			cin >> timeRequired;
			cout << "Recipe added." << endl;
			meow.push_back(new SingleRecipe(name_input, ingredient_input, method_input, servings, timeRequired));
			three.addRecipe(new SingleRecipe(name_input, ingredient_input, method_input, servings, timeRequired));
		}
		else if (input == 2)
		{
			string remove_input;
			cout << "Enter the name of the recipe you wish to remove: " << endl;
			getline(cin, remove_input);
			for (int i = 0; i < (signed int)meow.size(); i++)
			{
				if (meow[i]->getName() == remove_input)
				{
					meow.erase(meow.begin() + i);
					cout << "Remove successful." << endl;
				}
			}
			cout << "Recipe does not exist. Remove unsuccessful." << endl;
		}
		else if (input == 3)
		{
			double time_input;
			cout << "Please enter the time for searching: ";
			cin >> time_input;
			vector<SingleRecipe*> hate;
			hate.push_back(three.returnListOfRecipes(time_input));
			if (!hate.empty())
			{
				cout << "List of recipes: " << endl;
				for (int i = 0; i < (signed int)hate.size(); i++)
				{
					cout << hate[i]->toString() << endl;
				}
			}
			else
			{
				cout << "Recipe does not exist." << endl;
			}
		}
		else if (input == 4)
		{
			break;
		}
	}
	return 0;
}
[code]

At Main.cpp, line 77, I get this error (I'm using VS2012):
[code]
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: (std::vector<SingleRecipe *, std::allocator<SingleRecipe *>>)
            object type is: std::vector<SingleRecipe *, std::allocator<SingleRecipe *>> 


I tried changing pointers, but in the end the errors still came back to here.
When I compile, this happens:
 
'void std::vector<_Ty>::push_back(SingleRecipe *&&)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'SingleRecipe *&&'


I understand that there's an error trying to convert the vector to my class, but I don't know how to fix the conversion.

(I also might be somewhat uneducated about vectors, the internet doesn't help much in simplifying)

Sorry if it's a long wall of text, but I think I might need more knowledge in vectors that can be properly placed in typical layman terms for me to understand (yes, I'm interested in C++, but I need to understand before I can go into complexity)
The problem is push_back only takes a single variable of whatever type you specified when you created the vector. Your function "returnListOfRecipes" returns an entire vector (not just 1 element of a vector) so you can not use push_back in this way.

In this case you want to transfer an entire vector over, the easiest way to do this in this situation is.
vector<SingleRecipe*> hate = three.returnListOfRecipes(time_input);
Yeah, I did just that (thanks for your help), but now whenever I run my program, it doesn't seem to take in the vector when I run input == 3 after adding a vector (input == 1, adding a recipe in) (vector subscript out of range).

I understand this happens when the vector does not have items to begin with, but I thought I had already pushed it back into the vector?
Last edited on
This line:
for (int i = 0; i = recipe.size(); i++)

Should be:
for (int i = 0; i < recipe.size(); i++)

The way you have it now, you set i equal to the size, which is already out of bounds.
Oh.

Well, I just made such a terrible mistake. Sorry about that....
Thank you SO MUCH. I really appreciate that.
Topic archived. No new replies allowed.