我不知道是什么导致了这个错误:

资料来源如下:
#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;
}我对向量的经验基本上没有,但我不明白为什么它说它不能转换类型。向量被定义为类型成分,我试图推送给它的值也是相同类型的。
编辑:我很抱歉,这不是我想发布的错误,我只是更新了一下。之前的代码是在玩了一些东西之后。我确实意识到,在int类型的帽子代码中存在着差异。
发布于 2014-01-21 09:11:20
一个常见的错误:
ingredient defIngredient();不是用空构造函数初始化。
使用
ingredient defIngredient; 而不是。这是唯一的例外,非常令人讨厌和容易被忽视。
若要避免再次落入该陷阱,请使用新的{...}**-syntax**.初始化C++11和。
ingredient defIngredient{};不管怎么说。考虑在构造函数中使用初始化器:
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)
{}或使用C++11 成员初始化器
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说到C++11:
更好的老C++是:
void foo ( const ingredient& bar ) {
this->ingredients.push_back(bar);
}这可能会给你节省一份对象。
在C++11中,van对参数使用值语义(这总是一件好事),并且仍然保存副本--但是使用上面的const,您已经达到了90%的效果。
void foo ( ingredient bar ) {
this->ingredients.emplace_back(std::move(bar));
}也许这会让你对C++有所提升。
发布于 2014-01-21 08:58:28
ingredient.是一个vector<ingredient*>,如您所见,它包含指向vector<ingredient*>的指针。您正试图向后推bar,即a本身。
您不能只做&bar (很好,它会编译,但不会工作),因为bar是一个本地对象,很快就会被销毁。
相反,您应该将ingredient对象存储在矢量中:
vector<ingredient> ingredients;发布于 2014-01-21 08:57:54
您有指针向量,您尝试push_back的不是一个指向成分的指针,而是一个成分对象。从将vector<ingredient*> ingredients;更改为vector<ingredient> ingredients;开始。
https://stackoverflow.com/questions/21253302
复制相似问题