Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interpret the following error messages in C++

I have no experience with C++ and was given a code that should supposedly be able to run without any debugging, however, I get a bunch of errors when I try to compile it. I just need some direction on how to start debugging it, any help would be appreciated. Here is the error message:

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

Code relevant to error messages:

    // 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
like image 578
Isopycnal Oscillation Avatar asked Apr 17 '13 00:04

Isopycnal Oscillation


1 Answers

If you read the first error message:

invalid use of incomplete type ‘std::ifstream’

you'll see that the type std::ifstream hasn't been defined. It's defined in the header <fstream>, so you need to include that.

The code also uses std::istringstream, which requires <sstream>.

Most of the other errors should be fixed by that.

Also, although it doesn't cause an error, you should avoid using reserved names like __PARAMETER_FILE_PARSER__. In general, don't use leading or double underscores.

like image 73
Mike Seymour Avatar answered Nov 05 '22 19:11

Mike Seymour