微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

类保护区中命名空间的实验

如何解决类保护区中命名空间的实验

#include <iostream>

class A
{
public:
    int func();

protected:
    namespace B { enum { D = 0,E = 1 }; }
    namespace C { enum { D = 0,E = 1 }; }
}

int A::func()
{
    int x = A::B::D;
    int y = A::C::E;
    return x + y;
}

int main() {
    A a;
    int x = a.func();
    std::cout << x << std::endl; // 1
    
    return 0;
}

这段代码有什么问题?

我只是好奇并尝试使用 namespace,因为我希望 enums 具有相同的值名称

我不想使用 enum class,因为我无法在不重载运算符或强制转换的情况下对整数进行运算。

解决方法

您不能在类中声明命名空间。相反,您可以使用像

这样的结构
#include <iostream>

class A
{
public:
    int func();

protected:
    struct B { enum { D = 0,E = 1 }; };
    struct C { enum { D = 0,E = 1 }; };
};

int A::func()
{
    int x = A::B::D;
    int y = A::C::E;
    return x + y;
}

int main() {
    A a;
    int x = a.func();
    std::cout << x << std::endl; // 1
    
    return 0;
}

您可以在此 live example 中看到它的作用。

,

来自 C++ 14 标准(7.3.1 命名空间定义)

2 每个命名空间定义都应出现在全局范围或 命名空间范围

所以这些命名空间定义在类范围内(您忘记放置分号的地方)

attributes = { 'id' : 'id','volume' : ('volume','+') }

upsertVertexDataFrame(df=df,vertexType=type,attributes=attributes)

无效。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。