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 - 操作数大小非法
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01