c++ - 尝试调用将数据添加到 vector 的函数时出错

标签 c++ class vector

我收到一个错误,我认为这是由我编写的函数引起的,该函数将文件中的数据存储在我的类中名为 store in store.cpp 的 vector 中。

main.cpp

#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include "store.h"

using namespace std;

int main ()
{
    store data;
    ifstream inFile ("C:/Users/Owner/Desktop/Albums.csv");
    string line;
    string item;
    int num;
    int itemnum;
    int linenum = 0;
    while (getline (inFile, line))
    {
        linenum++;
        cout << endl << "Line #" << linenum << ":" << endl;
        istringstream linestream(line);
        itemnum = 0;
        num = 0;
        while (getline (linestream, item, ','))
        {
            itemnum++;
            if (itemnum == 2 || itemnum == 3 || itemnum == 4 || itemnum == 6)
            {
                num++;
                data.addtovect(linenum, num, item);
            }
            cout << "Item #" << itemnum << ": " << item << endl;
        }
    }
    return 0;
}

商店.h

#ifndef STORE_H
#define STORE_H

#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

class store
{
    public:
        store();
        void addtovect(int, int, string);
        void print();

    private:

        vector< vector<string> > fullline();
};

#endif

商店.cpp

#include "store.h"

store::store()
{

}
void store::addtovect(int a, int b, string c)
{
    fullline[a][b].push_back(c);
}
void store::print()
{

}

错误是:

C:\Users\Owner\Desktop\C++ projects\csc125\lab4\store.cpp|9|error: invalid types '<unresolved overloaded function type>[int]' for array subscript|.

我尝试寻找修复它的方法,但我并没有真正接近解决它。我看到的一些东西说错误是由 vector 与函数混淆引起的,但我不知道如何修复它。

最佳答案

在您的头文件中,您将 fullline 定义为一个函数,该函数返回一个包含包含字符串的 vector 的 vector :

vector< vector<string> > fullline();

删除括号,它将变成一个包含包含字符串的 vector 的 vector 。

vector< vector<string> > fullline;

编辑:
当您尝试将字符串添加到您的 fullline vector 时,您会遇到一些问题:
1) 未创建内部 vector ,您有时必须将一个 vector push_back 放入第一个 vector 中。
2)你正试图将你的字符串插入一个字符串:

fullline[a][b].push_back(c);

fullline < This is your first vector.
fullline[a] < Here you get the secondary vector.
fullline[a][b] < Here you get the object within the second vector (a string).
fullline[a][b].push_back(c) < Here you try to push back c into the string.

关于c++ - 尝试调用将数据添加到 vector 的函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25830275/

相关文章:

R:data.frame 中的 ne-name 因子值

C++ 私有(private)结构和非静态常量变量初始化

java - JNA:结构类中getFieldOrder()的目的是什么

c++ - const vector<T> * 哪一部分是const?

c++ - 将 userData 从回调开始传递到回调结束

c++ - C 在方程内部溢出?

c++ - 计算三角形网格中的法线

c++ - 为什么允许我为包含常量指针的 vector 分配新地址?

c++ - 追求者淘汰

c++ - OpenGL:指定三角形的意外顶点位置