【发布时间】:2019-07-26 18:51:25
【问题描述】:
[我的代码很大,所以我只粘贴了与我相关的部分,如果您需要查看更多内容,请告诉我,谢谢]
我在一个类(动画)中定义了一个常量,但现在我将该类一分为二(AnimationsDefinition 和 AnimationsInstance),我不能将该常量从一个类使用到另一个类
我在 AnimationsInstance.h 之前包含了 AnimationsDefinition.h
common.h
#include "animationsDefinition.h"
#include "animationsInstance.h"
编译器抱怨无法计算常量
下面的代码曾经可以工作,但现在我在另一个类中使用它,它不再工作了
我得到的错误:
error C2131: expression did not evaluate to a constant
note: failure was caused by non-constant arguments or reference to a non-constant symbol
note: see usage of 'EAST'
AnimationsDefinition.cpp
#include "common.h"
const int AnimationsDefinition::WEST = 0;
const int AnimationsDefinition::SOUTH = 1;
const int AnimationsDefinition::NORTH = 2;
const int AnimationsDefinition::EAST = 3;
...
AnimationsDefinition.h
#pragma once
class AnimationsDefinition
{
public:
static const int WEST;
static const int SOUTH;
static const int NORTH;
static const int EAST;
...
AnimationsInstance.cpp
#include "common.h"
void AnimationsInstance::update(float tpf)
{
switch (direction)
{
case AnimationsDefinition::EAST: <<<<<<<<< compilation error
{
...
任何帮助表示赞赏
问候
【问题讨论】:
-
在这里可以工作:godbolt.org/z/LolvUT
-
不错的应用,我去看看...
-
我将在另一个类中重新定义相同的常量,这不是很好,但我没有时间去了解 Visual Studio C++ 编译器的奇怪之处
-
您所做的似乎是枚举的常见用例。您是否有特定的原因决定不使用枚举来实现它?像这样:godbolt.org/z/9_1Z2R
-
“我的代码 [相当] 大...”正是您应该创建一个 minimal reproducible example 来演示问题的原因。