本文介绍了read_data代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨 我在下面写了这个例程,但是得到了它下面的错误,我不知道怎么解决。 谢谢 //useful.h / ** 将文件中的数据读入给定的类型向量。 @param字符串包含数据的文件的完整路径 @param short int列号在向量中返回 @param char type数据s-string,d-double,i-int ... @return vector< type_selected_as_above> * / 模板< class T> vector< Tget_data(字符串,短,字符); ******** ****************************************** ******** ****** //useful.cpp 模板< class Tvector< Tget_data(字符串s, 短sh, char ch){ sh--; ifstream in(s.c_str()); string line; vector< stringvs; vector< doublevd; vector< intvi; while(getline(in,line)){ stringstream input(line.c_str()); int j = 0; switch(ch){ case''s'': string st; while(input> st) if(sh == j ++)vspush_back(st); case''d'': double dt; while(输入> dt) if(sh == j ++)vd.push_back(dt); case''i'': int it; while(输入> it) if(sh == j ++)vi.push_back(it); 默认值: cout<< 未知类型 << endl; } } switch(ch){ case''s'':return vs ; 案例''d'':返回vd; 案例''我'':返回vi; } } ************************************* ************* ************** int main(){ string f =" /data/home/activity.dat" ;; vector< stringvss = get_data(f,1,'s''); //第40行错误 typedef vector< string> :: iterator ITER; ITER iterEnd = vss.end(); for( ITER iter = vss.begin(); iter!= iterEnd; ++ iter) copy(iter-> begin(),iter-> end(),ostream_iterator< string>(cout ,endl)); ************************************* ************* ************** main.cpp:40:错误:没有匹配函数来调用 ''get_data(std :: string&,int,char)'' main.cpp:46:错误:没有匹配函数来调用 ''std :: ostream_iterator< std :: basic_string< char,std :: char_traits< char>, std :: allocator< char,char,std :: char_traits< ;炭> Hi I wrote this routine below but getting the error below it which Idon''t know how to fix. thanks//useful.h/**Reads data from file into a given type vector.@param string full path to file containing the data@param short int column number to return in a vector@param char type of data s-string, d-double, i-int...@return vector<type_selected_as_above>*/template<class T>vector<Tget_data(string, short, char); ************************************************** **************//useful.cpp template<class Tvector<Tget_data(string s,short sh,char ch){sh--;ifstream in(s.c_str());string line;vector<stringvs;vector<doublevd;vector<intvi;while( getline(in, line) ){stringstream input(line.c_str());int j = 0;switch (ch){case ''s'':string st;while( input >st)if(sh == j++) vs.push_back(st);case ''d'':double dt;while( input >dt)if(sh == j++) vd.push_back(dt);case ''i'':int it;while( input >it)if(sh == j++) vi.push_back(it);default:cout << "type not known" << endl;}}switch(ch){case ''s'':return vs;case ''d'':return vd;case ''i'':return vi;}}************************************************** **************int main(){string f = "/data/home/activity.dat";vector<stringvss = get_data(f, 1, ''s''); //line 40 errortypedef vector<string>::iterator ITER;ITER iterEnd = vss.end();for (ITER iter = vss.begin(); iter != iterEnd; ++iter)copy(iter->begin(), iter->end(), ostream_iterator<string>(cout, endl));************************************************** **************main.cpp:40: error: no matching function for call to''get_data(std::string&, int, char)'' main.cpp:46: error: no matching function for call to''std::ostream_iterator<std::basic_string<char, std::char_traits<char>,std::allocator<char, char, std::char_traits<char> 推荐答案 奇怪:sh仅用于此声明,而后者又没有进一步的 后果。 Strange: sh is only used in this statement, which in turn has no furtherconsequences. 你不能这样做:一个函数只能在签名中返回指定类型的值 。您没有(也不能)定义一个返回 模板的函数。你定义了一个模板函数,即由模板参数参数化的函数族。 You can''t do that: a function can only return a value of the type specifiedin its signature. You did not (and cannot) define a function that returns atemplate. You defined a template function, i.e., a family of functionsparameterized by the template parameters. 重载决策不基于返回类型。因此,编译器没有想法为什么它应该看一下模板化的get_data()函数。你需要把它称为:b / b vector< stringvss = get_data< string>(f,1,''s''); Overload resolution is not based on return types. Thus, the compiler has noidea why it should have a look at the templated get_data() function. Youwould need to call it like: vector<stringvss = get_data<string>( f, 1, ,''s'' ); 或许,您希望endl为\ n。 Best Kai-Uwe Bux Probably, you want the endl to be "\n".Best Kai-Uwe Bux 奇怪:sh仅用于此声明,而后者又没有进一步的 后果。 Strange: sh is only used in this statement, which in turn has no furtherconsequences. 你不能这样做:一个函数只能在签名中返回指定类型的值 。您没有(也不能)定义一个返回 模板的函数。你定义了一个模板函数,即由模板参数参数化的一系列函数 。 You can''t do that: a function can only return a value of the type specifiedin its signature. You did not (and cannot) define a function that returns atemplate. You defined a template function, i.e., a family of functionsparameterized by the template parameters. 那么return语句会是什么样子? 返回向量< TvT; 然后我必须修改代码才能使用vT而不是3个不同的 矢量基于所选的类型。我是在正确的轨道吗? 模板< class Tvector< Tget_data(字符串s, 短片, char ch ){ if(sh)sh--; // index小于数字选择 ifstream in(s.c_str()); string line; vector< TvT; while(getline(in,line)){ stringstream input(line.c_str()); int j = 0; 开关(ch){ case''s'': string st; while(输入> st) if(sh == j ++)vT.push_back(st); case''d'': double dt; while(输入> dt) if(sh == j ++)vT.push_back(dt); case''i' ': int it; while(输入> it) if(sh == j ++)vT.push_back(it); 默认: cout<< 未知类型 <<结束; } } 返回vT; } } 但是这给了我..... /main.cpp:43:未定义的引用 到`std :: vector< std :: basic_string< char,std :: char_traits< char>, std :: allocator< char,std :: allocator< std :: basic_string< char, std :: char_traits< char> ;,std :: allocator< char get_data< std :: basic_string< char,std :: char_traits< char>, std :: allocator< char>(std :: basic_string< char, std :: char_traits< char>,std :: allocator< char,short,char)'' main()中有问题的行是这个 向量< stringvss = get_data< string>(f,1,'s''); so what would the return statement look like? return vector<TvT; then I have to modify the code to use vT and not 3 differentvectors based on the type selected. am I in the right track? template<class Tvector<Tget_data(string s,short sh,char ch){if(sh) sh--; //index is less than number selectionifstream in(s.c_str());string line;vector<TvT;while( getline(in, line) ){stringstream input(line.c_str());int j = 0;switch (ch){case ''s'':string st;while( input >st)if(sh == j++) vT.push_back(st);case ''d'':double dt;while( input >dt)if(sh == j++) vT.push_back(dt);case ''i'':int it;while( input >it)if(sh == j++) vT.push_back(it);default:cout << "type not known" << endl;}}return vT;}} but this gives me...../main.cpp:43: undefined referenceto `std::vector<std::basic_string<char, std::char_traits<char>,std::allocator<char, std::allocator<std::basic_string<char,std::char_traits<char>, std::allocator<char get_data<std::basic_string<char, std::char_traits<char>,std::allocator<char >(std::basic_string<char,std::char_traits<char>, std::allocator<char, short, char)'' the line in question in the main() is this vector<stringvss = get_data<string>(f, 1, ''s''); 奇怪:sh仅用于此声明,而后者又没有进一步的后果。 Strange: sh is only used in this statement, which in turn has no furtherconsequences. 你不能这样做:一个函数只能在其签名中返回指定类型的值。您没有(也不能)定义一个返回模板的函数。你定义了一个模板函数,即由模板参数参数化的一系列函数。 You can''t do that: a function can only return a value of the type specifiedin its signature. You did not (and cannot) define a function that returns atemplate. You defined a template function, i.e., a family of functionsparameterized by the template parameters. 那么return语句会是什么样的? 返回向量< TvT; 然后我必须修改代码才能使用vT而不是3个不同的 向量在选择的类型上。我是在正确的轨道吗? 模板< class Tvector< Tget_data(字符串s, 短片, char ch ){ if(sh)sh--; // index小于数字选择 ifstream in(s.c_str()); string line; vector< TvT; while(getline(in,line)){ stringstream input(line.c_str()); int j = 0; 开关(ch){ case''s'': string st; while(输入> st) if(sh == j ++)vT.push_back(st); case''d'': double dt; while(输入> dt) if(sh == j ++)vT.push_back(dt); case''i' ': int it; while(输入> it) if(sh == j ++)vT.push_back(it); 默认: cout<< 未知类型 <<结束; } } 返回vT; } } 但是这给了我..... /main.cpp:43:未定义的引用 到`std :: vector< std :: basic_string< char,std :: char_traits< char>, std :: allocator< char,std :: allocator< std :: basic_string< char, std :: char_traits< char> ;,std :: allocator< char get_data< std :: basic_string< char,std :: char_traits< char>, std :: allocator< char>(std :: basic_string< char, std :: char_traits< char>,std :: allocator< char,short,char)'' main()中有问题的行是这个 向量< stringvss = get_data< string>(f,1,'s''); so what would the return statement look like?return vector<TvT;then I have to modify the code to use vT and not 3 differentvectors based on the type selected. am I in the right track?template<class Tvector<Tget_data(string s, short sh, char ch){ if(sh) sh--; //index is less than number selection ifstream in(s.c_str()); string line; vector<TvT; while( getline(in, line) ){ stringstream input(line.c_str()); int j = 0; switch (ch){ case ''s'': string st; while( input >st) if(sh == j++) vT.push_back(st); case ''d'': double dt; while( input >dt) if(sh == j++) vT.push_back(dt); case ''i'': int it; while( input >it) if(sh == j++) vT.push_back(it); default: cout << "type not known" << endl; } } return vT; }}but this gives me...../main.cpp:43: undefined referenceto `std::vector<std::basic_string<char, std::char_traits<char>,std::allocator<char, std::allocator<std::basic_string<char,std::char_traits<char>, std::allocator<char get_data<std::basic_string<char, std::char_traits<char>,std::allocator<char >(std::basic_string<char,std::char_traits<char>, std::allocator<char, short, char)''the line in question in the main() is this vector<stringvss = get_data<string>(f, 1, ''s''); 但即使解决了这个问题,它仍然不会做我的意思 它要做的因为 while(输入> st)会将输入流输入st,但是st是 a预定义类型,好吧,如果文件有第一列a $ b怎么办? $ b string但是恰好是两倍。 but even after fixing this problem, it still will not do what I meantit to do because thewhile(input >st) will put the stream in input into st, but st is ofa predefined type, well, what if the file has the first column astring but st happen to be double. 这篇关于read_data代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 14:19