无法在标题中引用枚举 [英] Cannot refer to enum in header
问题描述
你好,
我试图自学C ++并且对头文件不太了解。当我尝试按如下方式创建一个时,我得到了一个我不明白的奇怪错误。我试过谷歌搜索,但我对C ++编译/链接过程缺乏了解是一个障碍。
无论如何,这里是头文件(删除了不必要的代码)
GraphicsLib.h
Hello,
I trying to self-learn C++ and don't have much idea about header files. When I tried to create one as follows, I got a strange error that I do not understand. I have tried googling but my lack of understanding about C++ compiling/linking process is an obstacle.
Anyway, here is the header file (unnecessary code removed)
GraphicsLib.h
#ifndef GRAPHICS_LIB_INCLUDE
#define GRAPHICS_LIB_INCLUDE
enum BorderTypes
{
SINGLE_ALL,
DOUBLE_ALL,
SINGLE_SIDE_DOUBLE_TOP,
DOUBLE_SIDE_SINGLE_TOP
};
static void DrawMessageWithBox(std::string message, BorderTypes borderType, int length);
#endif // GRAPHICS_LIB_INCLUDE
GraphicsLib.cpp文件
The GraphicsLib.cpp file
include "GraphicsLib.h"
static void DrawMessageWithBox(string message, BorderTypes borderType, int length)
{
//Draw the box and the message
}
主程序
The main program
#include<iostream>
#include "GraphicsLib.h"
using namespace std;
int main()
{
DrawMessageWithBox("HELLO WORLD", BorderTypes.DOUBLE_ALL, 77);
return 0;
}
每当我尝试编译程序时,都会收到此错误 -
[错误]'''之前的预期主要表达式''令牌
如果我更换功能用这个调用,
Whenever I try to compile the program, I get this error -
"[ERROR] Expected primary-expression before '.' token"
If I replace the function call with this one,
DrawMessageWithBox("HELLO WORLD", DOUBLE_ALL, 77);
我得到以下错误 -
[警告]'void DrawCenteredMessageWithBox(std :: string,BorderTypes,int)'已使用但从未定义[默认启用]
在函数'main'中:
未定义引用`DrawCenteredMessageWithBox(std :: string,BorderTypes,int)'
有谁能告诉我我做错了什么?
谢谢。
I get the following error -
"[Warning] 'void DrawCenteredMessageWithBox(std::string, BorderTypes, int)' used but never defined [enabled by default]"
In function 'main':
"undefined reference to `DrawCenteredMessageWithBox(std::string, BorderTypes, int)' "
Can anyone please tell me what am I doing wrong?
Thanks.
推荐答案
In第一种情况,你不需要BorderTypes。
前缀你的枚举值。在第二种情况下,您可以在某处(未显示)调用DrawCenteredMessageWithBox
,但该函数从未被定义过。
[edit]
参见Jochen的解决方案条目。
[/ edit]
In the first case you do not need theBorderTypes.
prefix on your enum values. In the second case you have a call toDrawCenteredMessageWithBox
somewhere (not shown), but that function has never been defined.
[edit]
See also Jochen's Solution entry.
[/edit]
使用枚举你必须使用两个冒号名称与枚举者之间:
With enums you must use two colons between the name and the enumerator:
DrawMessageWithBox("HELLO WORLD", BorderTypes::DOUBLE_ALL, 77);
>
[Warning]'void DrawCenteredMessageWithBox(std :: string,BorderTypes,int)'已使用但从未定义[已启用]默认情况下]
函数'main':
未定义引用`DrawCenteredMessageWithBox(std :: string,BorderTypes,int)'
"[Warning] 'void DrawCenteredMessageWithBox(std::string, BorderTypes, int)' used but never defined [enabled by default]"
In function 'main':
"undefined reference to `DrawCenteredMessageWithBox(std::string, BorderTypes, int)' "
您无法访问源文件外的 static
函数。删除 static
限定符以获取对它的访问权限。
You cannot access static
functions outside their source file. Remove the static
qualifier in order to gain access to it.
这篇关于无法在标题中引用枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!