这是读取文件的函数
void read_function(istream& filename, vector< vector<double> >& v)
{
//vector<vector<double> > v;
if (filename)
{
string linea;
int i = 0;
while (getline(filename, linea))
{
v.push_back(vector<double>());
stringstream split(linea);
double value;
while (split >> value)
{
v.back().push_back(value);
}
}
}
};
主要功能
int main(int argc, char const *argv[]){
if (argc < 2)
{
cerr << "input file's name\n"<< endl;
}
string program_name = argv[0];
ifstream input;
input.open(argv[1]);
vector< vector<double> > array;
for (int i = 0; i < array.size(); i++)
{
for (int j = 0; j < array[i].size(); j++)
cout << read_function(argv[1], array) << '\t';
cout << endl;
}
return 0;
}
当我编译代码时,我收到以下错误消息
错误:从类型'const char *'cout << read_function(argv [1],array)<<'\ t'的表达式初始化'std :: istream&{aka std :: basic_istream&}'类型的引用无效
错误:传递'void read_function(std :: istream&,std :: vector>&)的参数1'void read_function(istream&filename,vector <vector>&v)
1 回答
请不要采取错误的方式,但是您在代码中所犯的错误看起来像这对于您的编程技能水平来说可能是一项非常困难的任务 .
你犯了几个错误:
你(有点)在
main
中声明input
但是尝试在read_function
中使用它您在
getline
中使用的参数不正确read_function
的第一个参数是ifstream
类型char*
getline
是ifstream
的成员函数read_function
什么都不返回,所以cout<<read_function(...)
不正确我建议在尝试使用更复杂的东西之前,比如
sstream
,fstream
或vector
,首先要了解如何调用函数,参数及其类型是什么,对象是什么以及如何访问其成员 .我纠正了你的错误,下面的代码只读取输入文件中的数字,没有字符 . 我认为这是你的目标,因为你使用了
double
向量 . 还有更优雅的方法来做到这一点,但我尽量保持尽可能接近你的代码 .码: