得到的阵列的长度。编译器错误。 [英] Getting the length of an array. Compiler errors.

查看:104
本文介绍了得到的阵列的长度。编译器错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过:

template <typename T,unsigned S> 
unsigned getArraySize(const T (&v)[S]) { return S; } 

在Motti的回答 http://stackoverflow.com/a/18078435/512225

但我得到这个消息:

错误C2265:'':引用一个零大小的数组是非法的。

error C2265: '' : reference to a zero-sized array is illegal

这有什么错我的编译器?

What's wrong with my compiler?

我给看看这个页面:
<一href=\"http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4b78bcef-4c33-42f1-a4c5-fb6f702ced0b/vs6-c-compile-error-using-getaddrinfo\" rel=\"nofollow\">http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4b78bcef-4c33-42f1-a4c5-fb6f702ced0b/vs6-c-compile-error-using-getaddrinfo
所以我想这个解决方案:

I gave a look at this page: http://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4b78bcef-4c33-42f1-a4c5-fb6f702ced0b/vs6-c-compile-error-using-getaddrinfo so I tried this solution:

template <typename T,unsigned S> 
unsigned getArraySize(const T v[S]) { return S; } 

这编译,但是当我尝试使用它:

this compiles, but when I try to use it:

double myX[2] = {7,3};
std::cout << getArraySize(myX) << std::endl; 

我得到一个编译错误:结果
错误C2783:无符号整型__cdecl getArraySize(常量T []):不能推导出模板参数为S

I get a compilation error:
error C2783: 'unsigned int __cdecl getArraySize(const T [])' : could not deduce template argument for 'S'

除了修改编译器,有一种解决方法,我可以用它来获取数组的大小?

Beside changing the compiler, is there a workaround I can use to get the array's size?

推荐答案

这可能是VC6的限制,你有没有尝试过其他的编译器?

This could be a limitation of VC6, have you tried other compilers?

这篇关于得到的阵列的长度。编译器错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆