8

私は 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
4

2 に答える 2

11

最初のエラー メッセージを読んだ場合:

invalid use of incomplete type ‘std::ifstream’

std::ifstreamタイプが定義されていないことがわかります。header<fstream>で定義されているので、それを含める必要があります。

このコードでは も使用していますがstd::istringstream、これには が必要<sstream>です。

他のほとんどのエラーはそれによって修正されるはずです。

また、エラーにはなりませんが、 のような予約名の使用は避けてください__PARAMETER_FILE_PARSER__。一般に、先頭または二重のアンダースコアは使用しないでください。

于 2013-04-17T00:51:26.983 に答える