1

i am trying to make a program that reads from an ini file but in Visual Studio I keep getting compile errors about string identifiers and semicolons in the wrong place. I am not very good at C++ so it's most likely something really simple, but any way here is the code and error list.

INI.h

#ifndef INI_H
#define INI_H

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>

string INIreader(const string&, const string&);

#endif

INI_readnwrite.cpp

#include "INI.h"

using boost::property_tree::ptree;

string INIreader(const string& section, const string& name)
{
    ptree pt;
    read_ini("GameSettings.ini", pt);
    string value = pt.get<string>("section.name");
    return value;
}

mainCode.cpp

#include "INI.h"

using namespace std;
int main()
{
    string sec = "gameSettings";
    string val = "resXY";
    cout << INIreader(sec, val);
}

And here is the error list

error// file//  line//
error C2872: 'string' : ambiguous symbol    maincode.cpp    18
error C2146: syntax error : missing ';' before identifier 'sec' maincode.cpp    18
error C2065: 'sec' : undeclared identifier  maincode.cpp    18
error C2872: 'string' : ambiguous symbol    maincode.cpp    19
error C2146: syntax error : missing ';' before identifier 'val' maincode.cpp    19
error C2065: 'val' : undeclared identifier  maincode.cpp    19
error C2065: 'val' : undeclared identifier  maincode.cpp    20
error C2065: 'sec' : undeclared identifier  maincode.cpp    20
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   ini_readnwrite.cpp  6
error C2872: 'string' : ambiguous symbol    ini_readnwrite.cpp  6
error C2146: syntax error : missing ';' before identifier 'INIreader'   ini_readnwrite.cpp  6
error C2143: syntax error : missing ',' before '&'  ini_readnwrite.cpp  6
error C2086: 'int string' : redefinition    ini_readnwrite.cpp  6
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   ini_readnwrite.cpp  7
error C2974: 'boost::property_tree::basic_ptree<std::string,std::string,std::less<Key>>::get' : invalid template argument for 'Type', type expected ini_readnwrite.cpp  10
error C2974: 'boost::property_tree::basic_ptree<std::string,std::string,std::less<Key>>::get' : invalid template argument for 'Ch', type expected   ini_readnwrite.cpp  10
error C2146: syntax error : missing ';' before identifier 'value'   ini_readnwrite.cpp  10
error C2065: 'value' : undeclared identifier    ini_readnwrite.cpp  10
error C2065: 'value' : undeclared identifier    ini_readnwrite.cpp  11
IntelliSense: identifier "string" is undefined  INI.h   9
IntelliSense: identifier "string" is undefined  INI.h   9
IntelliSense: identifier "string" is undefined  INI.h   9
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   ini.h   9
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   ini.h   9
error C2146: syntax error : missing ';' before identifier 'INIreader'   ini.h   9
error C2146: syntax error : missing ';' before identifier 'INIreader'   ini.h   9
error C2143: syntax error : missing ',' before '&'  ini.h   9
error C2143: syntax error : missing ',' before '&'  ini.h   9

Thanks.

Aaryn
  • 33
  • 4

1 Answers1

0

You need to use the fully qualified name when you attempt to use std::string in your header file:

std::string INIreader(const std::string&, const std::string&);

And do the same thing in INI_readnwrite.cpp, or add a "using" directive like you did in the other .cpp file:

using boost::property_tree::ptree;
using namespace std;
sdzivanovich
  • 740
  • 3
  • 14
  • There's a reason that **[Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)** has "only" 660+525 upvotes. Read it! Also read the [second most voted answer](http://stackoverflow.com/a/1453605/85371), I'd say. – sehe Nov 20 '14 at 07:32
  • Hey, I was just wondering why my Visual studio was not underlining string calls when i used it to declare my function in INI.h? – Aaryn Nov 20 '14 at 20:43
  • @Aaryn, based on the error list it looks like Visual Studio did identify the problematic function declaration:`IntelliSense: identifier "string" is undefined INI.h 9`. I'm not sure why it wasn't underlined, sometimes Visual Studio has trouble detecting errors before compilation. – sdzivanovich Nov 22 '14 at 20:46