I cannot for the life of me figure out what is causing this error:
CMD Line error capture
Source below:
#include <iostream>
#include <vector>
using namespace std;
class taco {
private:
//Classes
class ingredient {
private:
//Ingredients have basic flavors
//Flavors should total 100% for ingredients
float sweet;
float sour;
float bitter;
float salt;
float umami; //Difficult taste to define, generally synonomous with savory or meaty
//Ingredients have flavor modifiers
bool hot; //Is the ingredient served Hot or Cold?
short intensity; //How bland is the ingredient (100% for flavorful, 0% for flavorless)
short moist; //100% for liquid (sauces etc) higher % to represent juice meats or fruits
public:
ingredient ( float sweet = 30.0, float sour = 0.0, float bitter = 0.0, float salt = 60.0, float umami = 10.0, bool hot = false, float intensity = 30.0, float moist = 25.0) {
//Default ingredient is flour tortilla
this->sweet = sweet; //Grains typically have a sugary tast even though it might not be very strong
this->sour = sour;
this->bitter = bitter;
this->salt = salt; //Tortillas do have a salt content as well, it might not be very strong but this 60% represents a total out of the 100% flavor, the strength is determined by intensity
this->umami = umami; //Most foods have at least small amounts of umami. Its umami that gives two foods with similliar tastes, different flavors.
this->hot = hot; //While a steamed tortilla is good, tortillas can be served either steamed or unsteamed. We default to not steamed as steamed tortilla will be a different ingredient
this->intensity = intensity; //Tortillas are relatively bland. They have a destinctive taste and can affect the overall taste of a taco but the taste is not very intense
this->moist = moist; //Flour tortillas are not generally moist but if there was no water content they would be brittle and unbending. Steamed tortillas and corn tortillas are usually more moist.
}
};
//Vectors
vector<ingredient> ingredients; //Create vector to store ingredients
public:
taco () {
ingredient defIngredient();
this->ingredients.push_back(defIngredient);
}
void foo ( ingredient bar ) {
this->ingredients.push_back(bar);
}
};
int main ( void ) {
return 0;
}
My experience with vectors is basically nothing but I can't figure out why it is saying that it cannot convert the type. The vector is defined as type ingredient and the value I'm attempting to push to it is of the same type.
EDIT: I apologize, this isn't the error I meant to post, I've updated it a smidge. The previous code was after playing around with things. I do realize that there was a difference in type int hat code.
6 Answers 6
ingredients is a vector<ingredient*> - as you can see, it contains pointers to ingredient. You are trying to push back bar, which is an ingredient itself.
You can't just do &bar instead (well it would compile, but it won't work), because bar is a local object and will be destroyed very soon.
Instead, you should just store ingredient objects in your vector:
vector<ingredient> ingredients;
Comments
A common error:
ingredient defIngredient();
is not initializing with the empty constructor.
Use
ingredient defIngredient;
instead. It is The-One-Exception that is very nasty and easily overlooked.
To not fall into that trap again use C++11 and always initialize with the new {...}-syntax.
ingredient defIngredient{};
Anyway. Consider using initializers in your constructor:
class ingredient {
private:
...
ingredient()
: sweet(30.0), sour(0.0), bitter(0.0), salt(60.0), umami(10.0),
hot(false), intensity(30.0), moist(25.0)
{}
Or with C++11 member initializators
class ingredient {
private:
float sweet = 30.0f;
float sour = 0.0f;
float bitter = 0.0f;
float salt = 60.0f;
float umami = 10.0f;
bool hot = false;
short intensity = 30;
short moist = 25;
}; // no default c'tor needed anymore
Speaking of C++11:
The better old C++ is:
void foo ( const ingredient& bar ) {
this->ingredients.push_back(bar);
}
which may save you one copy of the object.
In C++11 you van use value-semantics (which is always a good thing) for the parameter and still save the copy -- but with the const-ref above you are already 90% the way.
void foo ( ingredient bar ) {
this->ingredients.emplace_back(std::move(bar));
}
Maybe that gives you a boost towards C++.
Comments
You have vector of pointers, and what you try to push_back is not a pointer to ingredient but ingredient object. Start from changing vector<ingredient*> ingredients; to vector<ingredient> ingredients;.
Comments
The vector actually has the wrong type. You define your vector as vector<ingredient*> (a vector containing pointers to ingredient) but instead of pushing a pointer you are pushing by value.
You can fix your code by either changing the vector to a vector<ingredient> (omiting the asterisk) or making the function foo take a pointer.
Comments
Your vector contains pointers :-
vector<ingredient*> ingredients; //Create vector to store ingredients
But you are trying to push an object :-
void foo ( ingredient bar ) {
this->ingredients.push_back(bar);
You maybe wanted vector to store actual object, not pointers to them?
Comments
when using user define type as element of vector , default constructor & copy constructor should be provided.
when using push_back, the first step is create a object using default constructor ,the second step is copy the currect element to the new object in the vecotor, which is unefficient,vector is a better way
when you create an object using operator new, you won't have to worry about the local variable
taco () {
ingredient *defIngredient = new ingredient();
this->ingredients.push_back(defIngredient);
}
remember to delete the objects in the destructor functions
~ingredient ()
{
for(vector<ingredient*>::iterator vec_iter = ingradients.begin();
vec_iter != ingradients.end(); ++ vec_iter)
{
if(*vec_iter)
{
delete *vec_iter;
*vec_iter = NULL;
}
}
}
notes: when the object the frequently copyed and used in other class, shared_ptr can be applied.
ingredient defIngredient();declares a function. Get rid of the()and read about vexing parse..