【问题标题】:'std::vector' : 'U64' is not a valid template type argument for parameter '_Ty'“std::vector”:“U64”不是参数“_Ty”的有效模板类型参数
【发布时间】:2015-02-12 19:19:10
【问题描述】:

我有一个标题 SomeDefines.h,其中包含

#define U64 unsigned long long
#define U16 unsigned short

在 Myclass.h 中,我 #include SomeDefines.h 在顶部。

在 Myclass 声明中,我有一个函数

bool someFunc(const std::vector<U64>& theVector);

和一个成员变量 tbb::atomic someNumber;

当我在 Visual Studio 2012 中编译时,出现错误

error: 'U64': undeclared identifier
error C2923: 'std::vector': 'U64' is not a valid template type argument for parameter '_Ty'

如果我将U64 替换为unsigned long long,错误就会消失

bool someFunc(const std::vector<unsigned long long>& theVector);

我以为编译器会看到#define U64 unsigned long long 并将U64 替换为unsigned long long

为什么U64 而不是U16 会出现这些错误?

【问题讨论】:

  • 顺便说一句,您可以使用typedef(或using)代替宏。
  • 您似乎遇到了包含守卫的冲突。修复它,然后重构以使用 typedef。
  • 您收到错误是因为 U64 那时没有定义。无论如何定义一个类型而不是宏。或者更好的是,使用 &lt;stdint.h&gt; 类型。
  • 注意:有一个 给你一个uint64_t
  • 您使用的是预编译头文件 (stdafx.h) 吗?如果是,请尝试将您的包含放在之后

标签: c++ templates vector compiler-errors


【解决方案1】:

#define 不是创建新类型的好主意。首选typedefusing

typedef unsigned long long U64;
// or:
using U64 = unsigned long long;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多