C2070 - illegal sizeof operand(C2070 - 操作数大小非法)
问题描述
以下代码对我来说很好:
The following code looks fine to me:
#include <stdio.h>
template <typename T>
struct A
{
static float m_kA[];
};
template <typename T>
float A<T>::m_kA[] = {1.0f, 2.0f, 3.0f};
int main()
{
printf("%d
",
sizeof(A<unsigned int>::m_kA) /
sizeof(A<unsigned int>::m_kA[0]));
return 0;
}
但是当我用 VC9 编译时出现以下错误
But when i compile with VC9 i get the following error
error C2070: 'float []': illegal sizeof operand
我希望这段代码能够编译.我错过了什么吗?有谁知道解决这种奇怪行为的方法(请注意,没有模板的完全相同的东西可以正常编译并输出 3).
I would expect this code to compile. Am i missing something? Does anyone know a way to fix this strange behavior (note that the exact same thing without the template compiles fine and outputs 3).
请注意,删除模板不是一个选项,我制作这个示例是为了重现我在代码中遇到的问题,我需要包含数组的类型作为模板.
Note that removing the template is not an option, i made this example to reproduce a problem that i'm having in a code where i need the type containing the array to be a template.
谢谢
推荐答案
http://ideone.com/3ssVi
它用 G++ 编译得很好.
it compiles fine with G++.
据我所知,它可能与此错误有关:
As far as i can see it can be related to this bug:
http://connect.microsoft.com/VisualStudio/feedback/details/759407/can-not-get-size-of-static-array-defined-in-class-template
这篇关于C2070 - 操作数大小非法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C2070 - 操作数大小非法
基础教程推荐
- 提升 ASIO 流缓冲 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- c++ STL设置差异 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
