我想使用结构从文本文件中读取数据并将其加载到矢量中。
所以我写了一个跟随来做这个,但我没能编译。我不知道出了什么问题。
<我做了什么>
835,5,0,0,1,1,8.994,0
(整数数组[3],整数,整数,整数,整数,整数,浮点数,布尔值)
2.我声明一个结构包含以下数据类型以将数据加载到向量中;
struct unfinished
{
int ans[3]; // contains answer
int max;
int st;
int ba;
int outs;
int tri;
float elapsed;
bool fin;
};
3.我写了一个代码来读取数据如下;
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct unfinished
{
int ans[3];
int max;
int st;
int ba;
int outs;
int tri;
float elapsed;
bool fin;
};
vector<unfinished> read_rec(istream & is)
{
vector<unfinished> rec;
int ans[3];
int max, st, ba, outs, tri;
float elap;
bool fini;
while (is >> ans[0] >> ans[1] >> ans[2] >> max >> st >> ba >> outs >> tri >> elap >> fini)
{
rec.emplace_back(ans[0], ans[1], ans[2], max, st, ba, outs, tri, elap, fini);
}
return rec;
}
int main(void)
{
ifstream infile("unfin_rec.txt");
auto unfin = read_rec(infile);
vector<unfinished>::const_iterator it;
for (it = unfin.begin(); it != unfin.end(); it += 1)
{
cout << it->ans[0] << it->ans[1] << it->ans[2] << "," << it->max << "," << it->st << "," << it->ba <<","<<it->outs<<","<<it->tri<<","<<it->elapsed<<","<<it->fin<< endl;
}
system("pause");
return 0;
}
我无法编译此代码。错误消息是:&gt; c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ xmemory0(600):错误C2661:'unfinished :: unfinished':没有重载函数需要10个参数
再一次,我无法弄清楚这个消息的含义。请帮忙!
感谢,
seihyung
答案 0 :(得分:1)
你需要添加一个构造函数,它接受10个参数并填充结构的成员,如下所示:
struct unfinished
{
unfinished(int a0, int a1, int a2,
int m, int s, int b, int o, int t,
float e, bool b):
max(m), st(s), ba(b), outs(o), tri(t), elap(e), fini(b) {
ans[0]=a0, ans[1]=a1, ans[2]=a2;
}
....
};