【问题标题】:Getting string compile errors in visual studio using the boost string lib使用 boost 字符串库在 Visual Studio 中获取字符串编译错误
【发布时间】:2014-11-20 04:53:01
【问题描述】:

我正在尝试制作一个从 ini 文件中读取的程序,但在 Visual Studio 中,我不断在错误的位置收到有关字符串标识符和分号的编译错误。我不太擅长 C++,所以它很可能是非常简单的东西,但这里有代码和错误列表。

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);
}

这是错误列表

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

谢谢。

【问题讨论】:

    标签: c++ string boost visual-studio-2013


    【解决方案1】:

    当您尝试在头文件中使用 std::string 时,您需要使用完全限定名称:

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

    在 INI_readnwrite.cpp 中做同样的事情,或者像在另一个 .cpp 文件中一样添加一个“使用”指令:

    using boost::property_tree::ptree;
    using namespace std;
    

    【讨论】:

    • Why is “using namespace std;” considered bad practice?“只有”660+525 票是有原因的。阅读!还要阅读second most voted answer,我会说。
    • 嘿,我只是想知道为什么我的 Visual Studio 在我使用它在 INI.h 中声明我的函数时没有为字符串调用加下划线?
    • @Aaryn,根据错误列表,Visual Studio 确实识别出了有问题的函数声明:IntelliSense: identifier "string" is undefined INI.h 9。我不知道为什么它没有下划线,有时 Visual Studio 在编译之前无法检测到错误。
    猜你喜欢
    • 2015-06-20
    • 2013-03-01
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多