c++ - 如何创建定义固定值类型的策略模板

标签 c++ compiler-errors strategy-pattern

我有一个 Visual Studio 2008 C++03 应用程序,我在其中向类 Foo 提供策略 FooTraits

struct FooTraits
{
    enum { FooVal = 1 };
};

template< typename traits >
class Foo
{
public:
    typedef typename traits::FooVal foo_val; // errors on this line

    void FooDo( UINT matrix[ foo_val ] ) { /*...*/ };
};

typedef Foo< FooTraits > Foot;

int main( int argc, char* argv[] )
{
    Foot f;
    UINT matrix[ Foot::foo_val ] = { /*...*/ };
    f.FooDo( matrix );
    return 0;
}

但是,我在 typedef 上遇到了一系列编译器错误:

error C2146: syntax error : missing ';' before identifier 'foo_val'
error C2838: 'FooVal' : illegal qualified name in member declaration
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

创建定义固定值的策略的正确方法是什么?

最佳答案

FooTraits::FooVal 不是类型。这是枚举的一个可能值。你不能 typedef 不是类型的东西。您需要 decltype(FooTraits::FooVal),或者为枚举命名。

关于c++ - 如何创建定义固定值类型的策略模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10654607/

相关文章:

c++ - 如何在设计层面移除 dynamic_cast

c++ - 如何将 Boost::ASIO 与用户层网络堆栈集成?

java - SSLSocketFactory编译错误

c# - 在 C# 中执行此通用抽象类的最佳方法?

c++ - 多线程绘图不流畅怎么解决?

c++ - 常量方法中的奇怪行为,可以修改变量

xcode - 使用未解析的标识符 'PresentationButton'

C++模式策略初始化

java - 策略设计模式

C++ 三角函数返回意外值