Reading text file

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
timmymorris91
Posts: 19
Joined: Tue Mar 18, 2008 9:43 pm
Location: Wagga Wagga, Australia

Reading text file

Post by timmymorris91 »

I am creating a 2d map editor and It saves the map as a text file.

This is my header files included

Code: Select all

#include <irrlicht.h>
#include <iostream>
#include <fstream>
#include <string>
I am using this code to read the text file

Code: Select all

  string line;
  ifstream myfile2 ("example.txt");

  if (myfile2.is_open())
  {
    while (! myfile2.eof() )
    {
      getline (myfile2,line);
      cout << line << endl;
    }
    myfile2.close();
  }

  else cout << "Unable to open file"; 
If I add this above code in a new blank project WITHOUT including the
Irrlicht header then it works but when I inlclude the Irrlicht Header File I get this in the output:

E:\Pokemon\PokeMap Editor\main.cpp(154) : error C2872: 'string' : ambiguous symbol
E:\Pokemon\PokeMap Editor\main.cpp(154) : error C2955: 'string' : use of class template requires template argument list
h:\irrlicht-1.4\include\irrstring.h(862) : see declaration of 'string'
E:\Pokemon\PokeMap Editor\main.cpp(154) : error C2133: 'line' : unknown size
E:\Pokemon\PokeMap Editor\main.cpp(154) : error C2512: 'string' : no appropriate default constructor available
E:\Pokemon\PokeMap Editor\main.cpp(154) : error C2262: 'line' : cannot be destroyed
E:\Pokemon\PokeMap Editor\main.cpp(161) : error C2780: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &,const _E)' : expects 3 arguments - 2 provided
c:\program files\microsoft visual studio\vc98\include\string(149) : see declaration of 'getline'
E:\Pokemon\PokeMap Editor\main.cpp(161) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_istre
am<_E,_Tr> &' from 'class std::basic_ifstream<char,struct std::char_traits<char> >'
E:\Pokemon\PokeMap Editor\main.cpp(161) : error C2784: 'class std::basic_istream<_E,_Tr> &__cdecl std::getline(class std::basic_istream<_E,_Tr> &,class std::basic_string<_E,_Tr,_A> &)' : could not deduce template argument for 'class std::basic_strin
g<_E,_Tr,_A> &' from 'class irr::core::string'
E:\Pokemon\PokeMap Editor\main.cpp(162) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class irr::core::string' (or there is no acceptable conversion)

sorry I know its a lot

Anyway I thing the string header included is interfering with the string
class in irrlicht, and the string type is mistaken for the Irrlicht string instead of the string included in the string header, and then it is using that type for the 'line' in the:

getline (myfile2,line);

causing an error.

how can I include the string header without it causing errors with the Irrlicht string class?
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Do you have this:

Code: Select all

using namespace core;
Anywhere in your program? String is a class in irr::core, so that could be causing you problems.
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

A good "best practice" regarding the C++ standard lib is to never open the namespace std. Instead always fully qualify types and objects by explicitly using them like std::string, std::cout. Names in std are short and generic and will likely collide with other names from other libraries like it is the case here with irr::core::string and std::string.
Post Reply