类中 vector 的 C++ vector

标签 c++ class vector

我有一个类,其中 double vector 存储如下:

class clsHalfphoneUnitJoinFeatures : public CBaseStructure
{
private:
    vector<double> m_content;
protected:
    virtual void ProcessTxtLine(string line);
public:
    vector<double> &Content();
    void Add(vector<double> &jf);
};

但是,当我想添加一个新的 double vector 时,它不起作用:

void clsHalfphoneUnitJoinFeatures::ProcessTxtLine(string line)
{
    line = CompactLine(line);
    if (line == "")
        return;

    int b = 0;
    int n = line.find("\t");
    string s = "";
    int idx = 0;
    vector<double>jf;
    jf.resize(16);
    int i = 0;

    for(;;)
    {
        if (n == -1)//if this is the last item in this line
        {
            s = line.substr(b,line.length()-b);
            jf[i++] = atof(s.c_str());
            break;
        }
        s = line.substr(b,n-b);
        jf[i++] = atof(s.c_str());      
        b = n+1;
        n = line.find("\t",b);
    }
    m_content.push_back(jf);
}

我得到的错误是

m_content.push_back(jf);

error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)': Conversion of parameter 1 from 'std::vector<_Ty>' in 'double &&' not possible

谁能告诉我哪里出错了?

谢谢!

最佳答案

jfm_content 具有相同的类型,您不能将 jf 作为 m_content 的元素推送.

尝试改变

m_content.push_back(jf);

收件人:

m_content = jf;

如果你想要一个 double 类型的 vector ,你需要将m_content声明为:

std::vector<std::vector<double> > m_content;

关于类中 vector 的 C++ vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17131259/

相关文章:

c++ - vector::删除()不工作

c++ - `std::set_intersection` 的 lambda 比较器

c++ - 迭代容器时可以使用 '<' 而不是 '!=' 吗?

c++ - 如何覆盖在 C++ 函数内部的另一个类中定义的虚方法

node.js - Nodejs 扩展 Sequelize 模型

c++ - 将 vector 中的每个项目与其他每个项目进行比较,同时删除一些元素?

c++ - std::basic_string 作为函数模板的参数不能从 const char* 推导出

Java - for 循环问题

python - Python 3 类创建的最佳实践

变量重新分配中的 C++11 内存释放