【发布时间】:2017-05-01 06:53:16
【问题描述】:
我正在尝试使用以下代码从文件中读取数据对。
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
//**** Opening data file ****
ifstream infile;
infile.open("reg_data_log.inp");
if (infile.is_open())
{
cout << "File successfully open" << endl;
}
else
{
cout << "Error opening file";
}
//**** Reading data ***
vector<pair<double, double> > proteins;
pair<double, double> input;
while (infile >> input.first >> input.second)
{
proteins.push_back(input);
}
//**** Show data in screen ****
cout << "Proteins analized: " << proteins.size() << endl;
for(unsigned int i=0; i<proteins.size(); i++)
{
cout << i.first << ", " << i.second << endl;
}
}
编译时出现以下信息
“65:13:错误:请求‘i’中的成员‘first’,它是非类类型‘unsigned int’”
“65:13: 错误:请求‘i’中的成员‘first’,它是非类类型‘unsigned int’”
我无法解决这个问题。有人可以帮忙吗?
【问题讨论】:
-
错误很明显。您在最后一个循环中使用
i。这是一个int。然而你尝试访问i.first。 -
您需要使用迭代器。例如:
for (auto i : proteins) {cout << i.first << ", " << i.second << endl;} -
迭代器给我一个编译器错误 63:7:警告:'auto' 在 C++11 中改变了含义;请删除它[-Wc++0x-compat]
-
@berkboy 你在使用 pre c++11 编译器吗?
-
重新。 “‘auto’改变含义”:升级你的编译器或使用
-std=c++11编译标志(如果它很旧,在你的版本中可能是-std=c++0x)。