一个关于枚举与类访问权限的问题

yfjok22 2016-08-16 04:41:06
各位大侠好,最近碰到个问题想请教一下
目前代码需要在主函数中定义一个枚举类型的变量,而该枚举类型是由某一各类中定义
简单的测试代码如下

#include <stdio.h>
class enumtest
{
public:
enum test
{
choice1,
choice2,
choice3,
choice4
};
};

int main()
{
enum enumtest::test a = choice1;
printf("Hello world");
}

该代码不能通过编译,报错为“choice1”在主函数中未定义。小弟对OOP的理解不是十分深刻,不知道这个问题是不是类的访问权限问题,如何修改才能正常通过编译
谢谢
...全文
388 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2016-08-17
  • 打赏
  • 举报
回复
一般来说:以下两个都可以 我想标准应该也是如此:
enum enumtest::test a = enumtest::test::choice1;
enum enumtest::test a = enumtest::choice1;

///enumtest.cpp 
#include <iostream>
using namespace std;
class enumtest
{
public:
    enum test
    {
        choice1,
        choice2,
        choice3,
        choice4
    };
};
 
int main()
{
    enum enumtest::test a = enumtest::test::choice1;
    cout <<a<<endl;
}
如果你文件是.c(c 文件) 那么自定义类型 不是一个作用域,:: 是非法的 如果是C++(.cpp,.cc,.cxx)应该两种都可以
060 2016-08-16
  • 打赏
  • 举报
回复
C语言中的枚举,不需要限定符

enum test
    {
        choice1,
        choice2,
        choice3,
        choice4
    };
int x = test::choice1;// 这行代码中的 test::是可以不需要的,在VC中,这一个编译警告。
int y = choice1; // 这行代码是正确的
列子汤问 2016-08-16
  • 打赏
  • 举报
回复
enumtest.C: In function ‘int main()’: 的错误是因为 int main() 函数中没有return.
引用
因为choice1照理说应该是enumtest::test这个枚举类型域下的变量,因为它是在enumtest::test下的大括号中定义的。 如果是按照这种理解,应按按版主之前说的,是enumtest::test::choice1 但事实上应该按照你说的enumtest::choice1,不知道这个该怎么解释
至于这个,是因为enum不是作用域,是用来申明枚举类型的关键字。 enum没有严格的scope界限。 所以C++ 11中引入了枚举类的概念,既enum class。
lingmu_wuda 2016-08-16
  • 打赏
  • 举报
回复
感觉这样确实违背了作用域的常识,但是枚举的特点就是这样吧。这样枚举还能起到声明const变量的作用
pengzhixi 2016-08-16
  • 打赏
  • 举报
回复
你这个需要去看下作用域和名称查找规则的标准文档。
yfjok22 2016-08-16
  • 打赏
  • 举报
回复
引用 6 楼 pengzhixi 的回复:
enum enumtest::test a = enumtest::choice1;
大侠,你这个写法确实通过了编译,而且结果也正确 不知能否帮忙分析下为什么要这么写 因为choice1照理说应该是enumtest::test这个枚举类型域下的变量,因为它是在enumtest::test下的大括号中定义的。 如果是按照这种理解,应按按版主之前说的,是enumtest::test::choice1 但事实上应该按照你说的enumtest::choice1,不知道这个该怎么解释
pengzhixi 2016-08-16
  • 打赏
  • 举报
回复
enum enumtest::test a = enumtest::choice1;
yfjok22 2016-08-16
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:

#include <stdio.h>
class enumtest
{
public:
	enum test
	{
		choice1,
		choice2,
		choice3,
		choice4
	};
};

int main()
{
	enum enumtest::test a = enumtest::test::choice1;
	printf("Hello world");
}
感谢版主大侠,按照你的提示,我修改了下代码,并重新用gcc编译,但还是无法通过,如下 enumtest.C: In function ‘int main()’: enumtest.C:16:36: error: ‘enumtest::test’ is not a class or namespace enum enumtest::test a = enumtest::test::choice1; 不知问题出在哪
paschen 版主 2016-08-16
  • 打赏
  • 举报
回复
enum enumtest::test a = enumtest::test::choice1;
赵4老师 2016-08-16
  • 打赏
  • 举报
回复
Qualified Names Syntax qualified-name : qualified-class-name :: name If a qualified-class-name is followed by the scope-resolution operator (::) and then the name of a member of either that class or a base of that class, then the scope-resolution operator is considered a qualified-name. The type of a qualified-name is the same as the type of the member, and the result of a qualified-name expression is the member. If the member is an l-value, then the qualified-name is also an l-value. For information about declaring qualified-class-name, see Type Specifiers or Class Names . The class-name part of a qualified-class-name can be hidden by redeclaration of the same name in the current or enclosing scope; the class-name is still found and used. See Scope for an example of how to use a qualified-class-name to access a hidden class-name. Note Class constructors and destructors of the form class-name :: class-name and class-name :: ~ class-name, respectively, must refer to the same class-name. A name with more than one qualification, such as the following, designates a member of a nested class: class-name :: class-name :: name Names and Qualified Names Names used with the binary scope-resolution operator (::) are called “qualified names.” The name specified after the binary scope-resolution operator must be a member of the class specified on the left of the operator or a member of its base class(es). Names specified after the member-selection operator (. or –>) must be members of the class type of the object specified on the left of the operator or members of its base class(es). Names specified on the right of the member-selection operator (–>) can also be objects of another class type, provided that the left-hand side of –> is a class object and that the class defines an overloaded member-selection operator (–>) that evaluates to a pointer to some other class type. (This provision is discussed in more detail in Class Member Access in Chapter 12.) The compiler searches for names in the following order, stopping when the name is found: Current block scope if name is used inside a function; otherwise, global scope. Outward through each enclosing block scope, including the outermost function scope (which includes function arguments). If the name is used inside a member function, the class’s scope is searched for the name. The class’s base classes are searched for the name. The enclosing nested class scope (if any) and its bases are searched. The search continues until the outermost enclosing class scope is searched. Global scope is searched. However, you can make modifications to this search order as follows: Names preceded by :: force the search to begin at global scope. Names preceded by the class, struct, and union keywords force the compiler to search only for class, struct, or union names. Names on the left side of the scope-resolution operator (::) can be only class, struct, or union names. If the name refers to a nonstatic member but is used in a static member function, an error message is generated. Similarly, if the name refers to any nonstatic member in an enclosing class, an error message is generated because enclosed classes do not have enclosing-class this pointers.
赵4老师 2016-08-16
  • 打赏
  • 举报
回复
Scope Resolution Operator: :: In C++, you can tell the compiler to use the global variable rather than the local variable by prefixing the variable with ::, the scope resolution operator. If you have nested local scopes, the scope resolution operator doesn't provide access to variables in the next outermost scope. It provides access to only the global variables. Example // Example of the scope resolution operator #include <iostream.h> int amount = 123; // A global variable void main() { int amount = 456; // A local variable cout << ::amount; // Print the global variable cout << '\n'; cout << amount; // Print the local variable } The example has two variables named amount. The first is global and contains the value 123. The second is local to the main function. The two colons tell the compiler to use the global amount instead of the local one.
小灸舞 2016-08-16
  • 打赏
  • 举报
回复

#include <stdio.h>
class enumtest
{
public:
enum test
{
choice1,
choice2,
choice3,
choice4
};
};

int main()
{
enum enumtest::test a = enumtest::test::choice1;
printf("Hello world");
}

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧