编译器错误 C2327
“symbol”:不是类型名称、静态或枚举数
嵌套类中的代码尝试访问不是类型名称、静态成员或枚举数的封闭类成员。
使用 /clr 进行编译时,C2327 的一个常见原因是属性与属性类型同名。
以下示例生成 C2327:
// C2327.cpp
int x;
class enclose {
public:
int x;
static int s;
class inner {
void f() {
x = 1; // C2327; enclose::x is not static
s = 1; // ok; enclose::s is static
::x = 1; // ok; ::x refers to global
}
};
};
如果类型名称被成员名称隐藏,则也可能发生 C2327:
// C2327b.cpp
class X {};
class S {
X X;
// try the following line instead
// X MyX;
X other; // C2327, rename member X
};
在以下情况下,需要完全指定参数的数据类型,也可能触发 C2327:
// C2327c.cpp
// compile with: /c
struct A {};
struct B {
int A;
void f(A a) { // C2327
void f2(struct A a) {} // OK
}
};
以下示例生成 C2327:
// C2327d.cpp
// compile with: /clr /c
using namespace System;
namespace NA {
public enum class E : Int32 {
one = 1,
two = 2,
three = 3
};
public ref class A {
private:
E m_e;
public:
property E E {
NA::E get() {
return m_e;
}
// At set, compiler doesn't know whether E is get_E or
// Enum E, therefore fully qualifying Enum E is necessary
void set( E e ) { // C2327
// try the following line instead
// void set(NA::E e) {
m_e = e;
}
}
};
}
以下示例演示当属性与属性类型同名时触发 C2327:
// C2327f.cpp
// compile with: /clr /c
public value class Address {};
public ref class Person {
public:
property Address Address {
::Address get() {
return address;
}
void set(Address addr) { // C2327
// try the following line instead
// set(::Address addr) {
address = addr;
}
}
private:
Address address; // C2327
// try the following line instead
// ::Address address;
};