C ++ Vector无法推送

时间:2014-01-21 08:54:53

标签: c++ vector

我不能为我的生活找出造成这个错误的原因:

CMD Line error capture

来源:

#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类型的代码存在差异。

6 个答案:

答案 0 :(得分:3)

ingredientsvector<ingredient*> - 正如您所看到的,它包含指向ingredient 的指针。您正试图推回bar ingredient 本身。

你不能只改为&bar(好吧它会编译,但它不起作用),因为bar是一个本地对象,很快就会被销毁。

相反,您应该只在向量中存储ingredient个对象:

vector<ingredient> ingredients;

答案 1 :(得分:2)

你有指针的向量,你尝试push_back不是指向成分但成分对象的指针。从将vector<ingredient*> ingredients;更改为vector<ingredient> ingredients;开始。

答案 2 :(得分:2)

该向量实际上具有错误的类型。您将向量定义为vector<ingredient*>(包含指向成分的指针的向量),而不是按指针推动指针。

您可以通过将向量更改为vector<ingredient>(省略星号)或使函数foo取指针来修复代码。

答案 3 :(得分:2)

常见错误:

ingredient defIngredient();

使用空构造函数进行初始化。

使用

ingredient defIngredient; 

代替。这是一个非常讨厌且容易被忽视的例外情况。

要再次陷入该陷阱,请使用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中,你对参数使用值语义(这总是一件好事)并且仍然保存副本 - 但是使用上面的const-ref你已经是90%了。

void foo ( ingredient bar ) {
    this->ingredients.emplace_back(std::move(bar));
}

也许这会让你对C ++有所推动。

答案 4 :(得分:1)

你的矢量包含指针: -

vector<ingredient*> ingredients; //Create vector to store ingredients

但是你想要推送一个对象: -

void foo ( ingredient bar ) {
    this->ingredients.push_back(bar);

你可能想要矢量存储实际对象,而不是指向它们的指针?

答案 5 :(得分:1)

当使用用户定义类型作为向量的元素时,默认构造函数&amp;应提供复制构造函数。

使用push_back时,第一步是使用默认构造函数创建对象,第二步是将currentct元素复制到vecotor中的新对象,这是效率低下,向量是更好的方法

使用operator new创建对象时,您不必担心局部变量

taco () {
    ingredient *defIngredient = new ingredient();
    this->ingredients.push_back(defIngredient);
}

记得删除析构函数中的对象

~ingredient ()
{
   for(vector<ingredient*>::iterator vec_iter = ingradients.begin();
                                     vec_iter != ingradients.end(); ++ vec_iter)
   {
        if(*vec_iter)
        {
            delete *vec_iter;
            *vec_iter = NULL;
        }
   }
}

注意:当对象频繁复制并在其他类中使用时,可以应用shared_ptr。