繁体   English   中英

类中向量的C ++向量

[英]C++ vector of vectors in class

我有一个类,其中像这样存储双精度向量:

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向量时,它将不起作用:

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

错误C2664:'无效std :: vector <_Ty> :: push_back(_Ty &&)':无法将参数1从'std :: vector <_Ty>'转换为'double &&'中的

有人可以告诉我我哪里出问题了吗?

谢谢!

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

尝试改变

m_content.push_back(jf);

至:

m_content = jf;

如果要使用double类型vector的vector ,则需要将m_content声明为:

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

a)错误m_content.push_back(jf); 您正在尝试将向量推到可以存储两倍的向量。 所以编译器给出了错误。

您可以通过将jf分配给m_context来解决它

m_content = jf;

b)否则,如果您的实现需要向量向量,请执行以下步骤

声明m_content作为double向量的向量。

vector<vector<double>> m_content;
 ...
 m_content.push_back(jf);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM