问题描述
#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)
无效。