简体   繁体   中英

C++ vector of vectors in class

I have a class in which vectors of doubles are stored like this this:

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

However, when I want to add a new vector of doubles, it won't work:

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

The error I am getting is in

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

Can somebody tell me where I went wrong?

Thank you!

jf and m_content have the same type, you can't push jf as an element of m_content .

Try change

m_content.push_back(jf);

To:

m_content = jf;

If you want to have a vector of vector of double type , you need to declare m_content as:

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

a) Error m_content.push_back(jf); . You are trying to push vector to a vector which can store double. so the compiler is giving error.

You can resolve it by assigning jf to m_context

m_content = jf;

b) Else if your implementation needs vector of vector follow the steps below

Declare m_content as vector of double vector.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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