c++ - 命名空间 'name' 中没有名为 'namespace' 的成员

标签 c++ namespaces

我一辈子都弄不明白为什么会出现这个错误,因为我很确定语法是正确的(显然我错了!)。所以我想看看这里是否有人可以为我指出这一点。

main.cpp

#include "Object.h"

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

对象.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, '|');

最佳答案

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

以下代码编译(同时使用 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;
}

希望对您有所帮助。

关于c++ - 命名空间 'name' 中没有名为 'namespace' 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26824559/

相关文章:

c++ - 为什么当派生类的析构函数是非虚函数时基类析构函数调用派生对象?

c++ - ROS 与 QtCreator : autocompletion

namespaces - Clojure 命名空间管理 - 有没有办法保存和恢复 clojure repl 命名空间、导入等的状态?

c++ - extern 在命名空间中如何工作?

python - 使用 Python globals() 处理动态类的最佳方法

c++ - 使用一个 Makefile 构建多个共享库

C++ std::vector<T*> 如果你持有一个指向指针元素的指针,它会在调整大小时变得无效吗?

c++ - 如何在qt中获取主窗口的touchevent

Python:删除子类中的类属性

c++ - 具有自定义返回类型的全局命名空间中的友元函数