[英]Error when trying to call a function that will add data to vector
I get an error that I believe is caused by the function I wrote to store data from a file in a vector in my class called store in store.cpp. 我收到一个错误,我认为该错误是由我写的用于将文件中的数据存储在我的类中的存储在store.cpp类中的函数引起的。
Main.cpp 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;
}
Store.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
Store.cpp Store.cpp
#include "store.h"
store::store()
{
}
void store::addtovect(int a, int b, string c)
{
fullline[a][b].push_back(c);
}
void store::print()
{
}
The error is: 错误是:
C:\\Users\\Owner\\Desktop\\C++ projects\\csc125\\lab4\\store.cpp|9|error: invalid types '<unresolved overloaded function type>[int]' for array subscript|.
C:\\ Users \\ Owner \\ Desktop \\ C ++ projects \\ csc125 \\ lab4 \\ store.cpp | 9 |错误:数组下标|的类型'<未解析的重载函数类型> [int]'无效。
I tried looking for a way to fix it, but I didn't really come any closer to figuring it out. 我试图寻找一种解决方法,但实际上并没有找到解决它的方法。 Some of the things I saw said that the error was caused by the vector being confused with a function, but I have no idea how I could fix it.
我看到的一些事情说错误是由向量与函数混淆引起的,但是我不知道如何解决。
In your header file you define fullline as a function that returns a vector containing vectors containing strings: 在头文件中,您将全角定义为一个函数,该函数返回一个包含包含字符串的向量的向量:
vector< vector<string> > fullline();
Remove the parentheses and it will instead be a vector containing vectors containing strings. 删除括号,它将改为一个包含包含字符串的向量的向量。
vector< vector<string> > fullline;
Edit: 编辑:
When you try to add the string to your fullline
vectors, you will have some issues: 当您尝试将字符串添加到
fullline
矢量时,您将fullline
一些问题:
1) The inner vector is not created, you will at some point have to push_back
a vector into the first vector. 1)未创建内矢量,会在某些点必须
push_back
载体导入第一矢量。
2) You are trying to push your string into a string: 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.
Your variable is 您的变量是
vector< vector<string> > fullline
fullline
is a vector, fullline[a]
is a vector, but fullline[a][b]
is a string. fullline
是向量, fullline[a]
是向量,而fullline[a][b]
是字符串。
string
is basically vector<char>, so it also has the push_back
method, but you can only push_back
a char to string. string
基本上是vector <char>,因此它也具有push_back
方法,但是您只能将一个char push_back
到字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.