现在大家手头可能都还是vs2005的beta1,但最初的beta1跟不上新的形势了。
下面是在Draft 1.5 中8.3节Parameter Array的例子
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine("\targs[{0}] = {1}", i, args[i]);
}
例子很简单,短小精悍,看起来比原来的C语法更好懂,你心里赞叹一番,然后将这段代码Copy到你的工程里的。踌躇满志的你将又要学通一门新的语言,那么通过编译,确定自己是否看懂了吧。 用的编译器是 vc2005 的 beta1
Ctrl+F5...片刻后
Error 1 error C2146: syntax error : missing ')' before identifier 'array'
Error 2 error C3646: 'array' : unknown override specifier
Error 3 error C2143: syntax error : missing ';' before '<'
Error 4 error C2059: syntax error : ')'
Error 5 error C2143: syntax error : missing ';' before '{'
Error 6 error C2447: '{' : missing function header (old-style formal list?)
Error 7 error C2061: syntax error : identifier 'array'
Error 8 error C2143: syntax error : missing ')' before '{'
Error 9 error C2143: syntax error : missing ';' before '{'
Error 10 error C2143: syntax error : missing ';' before '}'
Error 11 error C2059: syntax error : ')'
这是什么?!@#¥%^
沮丧的你在一遍一遍证明自己的输入绝对正确后,放弃自己寻求解决了。你的心情被弄坏了。心中想起了Stan Lippman对C++/CLI的评价,决定去找他算帐了。google一搜,还真找对人了,C++/CLI revision of the Managed Extension Reference Array Syntax 就在他的blog上贴着。看最后一段
In the original language design, there was no explicit support for the param array that C# supports. Instead, one flags an ordinary array with an attribute, as follows:
void Trace1( String* format, [ParamArray]Object* args[] );
void Trace2( String* format, Object* args[] );
While these both look the same, the ParamArray attribute tags this for C# or other
void Trace1( String^ format, ... array<Object^>^ args );
void Trace2( String^ format, array<Object^>^ args );
in which the ellipsis […] preceding the array declaration tags it as a param array. Unfortunately, this has recently failed to make the list of features to be implemented for the inaugural release of C++/CLI. However, I can’t bear to [at least as yet] remove it from the translation tool.
看来问题在于自己看的文档是草案啊,手头的编译器可能还没能实现所有!!!
// generated using an Application Wizard.
#include "stdafx.h"
using namespace System;
using namespace stdcli::language;
void F([ParamArray] array<int>^ args)
{
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine( "\targs[{0}] = {1}", i, args[i]);
}
void G([ParamArray] array<Object^>^ args)
{
Console::WriteLine("# of arguments: {0}", args->Length);
for (int i = 0; i < args->Length; i++)
Console::WriteLine( "\targs[{0}] = {1}", i, args[i]);
}
int _tmain()
{
// TODO: Please replace the sample code below with your own.
F();
F(1);
F(1, 2);
F(1, 2, 3);
F(gcnew array<int> {1, 2, 3, 4});
G(10, "Hello", 1.23, 'X');
}
OK了!
但你用的还是比较老的语法,这样不会让人感到愉快。
再google,找到了这个"Visual C++ 2005 Tools Refresh" http://www.microsoft.com/downloads/details.aspx?FamilyID=afd04ff1-9d16-439a-9a5e-e13eb0341923&displaylang=en 。下载后会有vcpp_40904_install.exe文件 ,安装它。先编译使用[ParamArray]语法的,呵会看到下面的提示
warning C4572: [ParamArray] attribute is deprecated under /clr, use '...' instead
这时再次编译draft1.5的代码,这次可以了。 一切正常!
实际上在没有更新前编译器前, draft 1.5中其实有很多的内容是编译不过的。 例如在 8.14 Attributes 这一节
这个原来就不能被编译通过,要改成
//或
Class1^ c = gcnew Class1();
Type^ type = c->GetType();
才能通过编译,升级编译器后这些问题也都被解决掉。