读书人

怎么定义两个相同的enum变量并且不会有

发布时间: 2012-02-03 22:02:47 作者: rapoo

如何定义两个相同的enum变量并且不会有重复定义的错误
在stack.h文件中[code=C/C++][/code]
extern const int maxsize = 20;

extern enum error_code {success, underflow, overflow};

typedef int stack_entry;
在queue.h中,[code=C/C++][/code]
enum error_code {success, underflow, overflow};
typedef int queue_entry;
extern const int maxsize;
出错如下:f:\c++ programs\3_1_e3\queue.h(1) : error C2011: 'error_code' : 'enum' type redefinition
应该怎么办?

[解决办法]

C/C++ code
#ifndef ERROR_CODE_DEFINED#define ERRPR_CODE_DEFINEDenum error_code {success, underflow, overflow};#endif
[解决办法]
使用名字空间

#include <iostream>   using namespace std;   namespace savitch1   {   void greeting( );   }   namespace savitch2   {   void greeting( );   }   void big_greeting( );   int main( )   {   {   using namespace savitch2; //使用savictch2、std、全局三个命名空间   greeting( );   }   {   using namespace savitch1; //使用savitch1、std、全局三个命名空间   greeting( );   }   big_greeting( ); //使用了std一个标准命名空间   return 0;   }   namespace savitch1   {   void greeting( )   {   cout << "Hello from namespace savitch1.\n";   }   }   namespace savitch2   {   void greeting( )   {   cout << "Greetings from namespace savitch2.\n";   }   }   void big_greeting( )   {   cout << "A Big Global Hello!\n";   }
[解决办法]
只需定义一个enum类型,再定义两个不同的该类型的变量。
[解决办法]
extern修饰变量、函数。没听说可以修饰类型。

读书人网 >C++ Builder

热点推荐