我没有使用C ++的经验,并且给出了一个应该能够在没有任何调试的情况下运行的代码,但是,当我尝试编译时,我遇到了一堆错误。我只是需要一些指导如何开始调试它,任何帮助将不胜感激。以下是错误消息:
mpicc -c -O3 curvilinear_grid.cpp
In file included from parameters.h:12:0,
from curvilinear_grid.h:13,
from curvilinear_grid.cpp:6:
parameter_file_parser.h: In member function ‘int PARAMETER_FILE_PARSER<T>::Parse_Parameter_File()’:
parameter_file_parser.h:86:13: error: invalid use of incomplete type ‘std::ifstream {aka struct std::basic_ifstream<char>}’
/usr/include/c++/4.6/iosfwd:113:11: error: declaration of ‘std::ifstream {aka struct std::basic_ifstream<char>}’
parameter_file_parser.h:92:29: error: no matching function for call to ‘getline(std::ifstream&, std::string&)’
parameter_file_parser.h:92:29: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.6/bits/basic_string.h:2734:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
parameter_file_parser.h:95:21: error: no match for ‘operator>>’ in ‘tokens >> std::ws’
parameter_file_parser.h:95:21: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
/usr/include/c++/4.6/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
/usr/include/c++/4.6/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
/usr/include/c++/4.6/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
/usr/include/c++/4.6/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
/usr/include/c++/4.6/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
parameter_file_parser.h:95:51: error: no matching function for call to ‘getline(std::istringstream&, std::string&, char)’
parameter_file_parser.h:95:51: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.6/bits/basic_string.h:2734:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
parameter_file_parser.h:96:14: error: no match for ‘operator>>’ in ‘tokens >> std::ws’
parameter_file_parser.h:96:14: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
/usr/include/c++/4.6/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
/usr/include/c++/4.6/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
/usr/include/c++/4.6/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
/usr/include/c++/4.6/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
/usr/include/c++/4.6/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
parameter_file_parser.h:96:41: error: no matching function for call to ‘getline(std::istringstream&, std::string&)’
parameter_file_parser.h:96:41: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.6/bits/basic_string.h:2734:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
parameter_file_parser.h:107:8: error: invalid use of incomplete type ‘std::ifstream {aka struct std::basic_ifstream<char>}’
/usr/include/c++/4.6/iosfwd:113:11: error: declaration of ‘std::ifstream {aka struct std::basic_ifstream<char>}’
make: *** [curvilinear_grid.o] Error 1
与错误消息相关的代码:
// File: parameter_file_parser.h
// Desc: Parser of the parameter input file to change default parameter values
#ifndef __PARAMETER_FILE_PARSER__
#define __PARAMETER_FILE_PARSER__
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
template<class T=double>
class PARAMETER_FILE_PARSER
{
public:
PARAMETER_FILE_PARSER(string filename) : parameter_filename(filename)
{param_map=NULL;}
~PARAMETER_FILE_PARSER() {if(param_map) delete param_map;}
int Parse_Parameter_File();
bool Get_Value(string key, int& value)
{
if(!param_map) return false;
map<string,string>::iterator i = (*param_map).find(key);
if(i != (*param_map).end()){
value = atoi(i->second.c_str());
return true;
}else return false;
}
bool Get_Value(string key, double& value)
{
if(!param_map) return false;
map<string,string>::iterator i = (*param_map).find(key);
if(i!=(*param_map).end()){
value = atof(i->second.c_str());
return true;
}else return false;
}
bool Get_Value(string key, string& value)
{
if(!param_map) return false;
map<string,string>::iterator i = (*param_map).find(key);
if(i!=(*param_map).end()){
string raw_val = i->second;
size_t ip1,ip2;
ip1 = raw_val.find('"');
ip2 = raw_val.find('"',ip1+1);
if(ip1!=string::npos && ip2!=string::npos){
value = raw_val.substr(ip1+1,ip2-ip1-1);
return true;
}else return false;
}else return false;
}
/*
bool Get_Value(string key, bool& value)
{
if(!param_map) return false;
map<string,string>::iterator i = (*param_map).find(key);
if(i!=(*param_map).end()){
switch(i->second){ //make lowercase
case "true": value = true;break;
case "false": value = false;break;
}
return true;
}else return false;
}else return false;
}
*/
bool Processed() {return param_map!=NULL;}
private:
map<string, string> *param_map;
string parameter_filename;
};
//*****************************************************************************
// Parsing parameter_filename and populating the parameters map
//*****************************************************************************
template<class T>
int PARAMETER_FILE_PARSER<T>::Parse_Parameter_File()
{
ifstream pfile(parameter_filename.c_str());
if (!pfile.is_open())
{cout << "Unable to open parameter file:parameter_filename"<<endl;return 0;}
param_map = new map<string, string>();
string line,key, value;
while (getline(pfile, line))
{
istringstream tokens(line);
while(tokens >> ws && getline(tokens, key, '=') &&
tokens >> ws && getline(tokens, value)){
key.erase(remove(key.begin(),key.end(),' '),key.end());
size_t i = value.find("//");
if(i!=string::npos) {
value = value.substr(0,i-1);
value.erase(remove(value.begin(),value.end(),' '),value.end());
}
(*param_map)[key] = value;
}
}
pfile.close();
return 1;
}
//*****************************************************************************
#endif
答案 0 :(得分:10)
如果您阅读了第一条错误消息:
invalid use of incomplete type ‘std::ifstream’
您会看到尚未定义类型std::ifstream
。它在标题<fstream>
中定义,因此您需要包含它。
该代码还使用了std::istringstream
,这需要<sstream>
。
大多数其他错误应该由此修复。
此外,虽然它不会导致错误,但您应该避免使用__PARAMETER_FILE_PARSER__
之类的保留名称。通常,不要使用前导或双下划线。
答案 1 :(得分:0)
我相信在您访问ifstream之前,您需要在标题中加入fstream。