【问题标题】:No member named 'name' in namespace 'namespace'命名空间“namespace”中没有名为“name”的成员
【发布时间】:2014-11-09 03:09:58
【问题描述】:

我一生都无法弄清楚为什么会产生这个错误,因为我很确定语法是正确的(显然我错了!)。所以我想我会看看这里是否有人可以为我指出。

ma​​in.cpp

#include "Object.h"

int main(){
    out = json::readJSON(data_dir + "a2-empty_array_with_empty_object.json", e, debug);
}

Object.h

namespace json{
template<typename T>
    std::string readJSON(std::string jsonFile, T& object, bool debug = false, char delimiter = ',') {}
}

当函数显然在命名空间中时,我基本上得到了这个错误。为什么将函数称为成员?也许这里还有其他事情发生......

错误:

a2main.cpp:66:21: error: no member named 'readJSON' in namespace 'json'
        out = json::readJSON(data_dir + "a2-cartoons.json", c, debug, '|');

【问题讨论】:

  • 确保您#includeing 正确的文件。
  • 我是,我已经检查了两次和三次。
  • 您是否尝试过限定模板,例如 readJson?我的第一个预感是模板问题。
  • 发布的代码中没有声明“data_dir”、“e”和“debug”名称。
  • 上述解决方案似乎都没有解决它。至于未声明的变量,我没有发布它们,因为它们似乎与问题无关。学习 C++ 的斗争!

标签: c++ namespaces


【解决方案1】:

您可能没有正确包含头文件。

以下代码编译(使用clang和gcc)并运行良好

#include <string>

namespace json
{

    template<typename T>
    std::string readJSON(std::string jsonFile, T& object, bool debug = false, char delimiter = ',') 
    {
       return "Hello"; //This function should return a string
    }

}

int main()
{
    std::string data_dir = "test-";
    int e = 3;
    bool debug = false;
    std::string out = json::readJSON(data_dir + "a2-empty_array_with_empty_object.json", e, debug);
    return 0;
}

我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多