1

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.

asked Jan 21, 2014 at 8:54
5
  • 1
    Give us a minimal complete example and we'll take a look. Commented Jan 21, 2014 at 8:56
  • 3
    ingredient defIngredient(); declares a function. Get rid of the () and read about vexing parse.. Commented Jan 21, 2014 at 9:05
  • Bagh I was trying to use the default constructor, Thanks, works now. Commented Jan 21, 2014 at 9:06
  • 1
    Without the parentheses you are still using the default constructor, you just can't use them if you aren't passing arguments because that parses as a function declaration. Commented Jan 21, 2014 at 9:08
  • Right, I was focusing on vectors cause I never used em I forgot to check my syntax on using classes 8-P I'm reviewing the syntax now. Thanks again Commented Jan 21, 2014 at 9:10

6 Answers 6

3

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;
answered Jan 21, 2014 at 8:58
Sign up to request clarification or add additional context in comments.

Comments

2

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++.

answered Jan 21, 2014 at 9:11

Comments

2

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;.

answered Jan 21, 2014 at 8:57

Comments

2

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.

answered Jan 21, 2014 at 8:58

Comments

1

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?

answered Jan 21, 2014 at 8:57

Comments

1

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.

answered Jan 21, 2014 at 9:03

3 Comments

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<ingredient*> is a better way
I've been thinking about your comment for a while. To me, even though it seems more efficient, it seems like the object creation would need to happen within the main function so that the objects can persist (if we create an object in a method within the taco class, the object would get destroyed when it's out of scope making the pointer worthless). Above, we create an object, put it in the vector and than allow the object to be destroyed at the end of scope while keeping a persistent copy in the vector. If you can show me a better way to do this I'd love to see it but I can't see it in my head
@Toby when you create an object using operator new, you won't have to worry about the local variable, I have add the way I would usually apply in the answer , hope it will help

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.