// CLR.cpp: 主项目文件。#include "stdafx.h"
using namespace System;value class Length
{
private:
int feet;
int inches;
public:
static initonly int inchesPerFoot = 12;
//构造函数 
Length(int ft,int ins):feet(ft),inches(ins){ }
//重载ToString方法
virtual String^ToString()override
{
return feet+L" feet "+inches+L" inches";
}
//重载"+"操作符
Length operator+(Length len)
{
int inchTotal = inches+len.inches+inchesPerFoot*(feet+len.feet);
return Length(inchTotal/inchesPerFoot,inchTotal%inchesPerFoot);
}
//重载"/"操作符 
static Length operator/(Length len,double x)
{
int ins = safe_cast<int>((len.feet*inchesPerFoot+len.inches)/x);
return Length(ins/inchesPerFoot,ins%inchesPerFoot);
}  
static Length operator*(double x,Length len); //double型做为被乘数
static Length operator*(Length len,double x); //double型做为乘数
};
Length Length::operator *(double x, Length len)
{
int ins = safe_cast<int>(x*len.inches+x*len.feet*inchesPerFoot);
return Length(ins/inchesPerFoot,ins%inchesPerFoot);
}
Length Length::operator*(Length len,double x)
{
return operator*(x,len);
}int main(array<System::String ^> ^args)
{

Length len1 = Length(6,9);
Length len2 = Length(7,8);
double factor = 2.5;
Console::WriteLine(L"{0} Plus {1} is {2}",len1,len2,len1+len2);
Console::WriteLine(L"{0} times {1} is {2]",factor,len2,factor*len2); //double型做为被乘数
Console::WriteLine(L"{1} times {0} is {2]",factor,len2,len2*factor); //double型做为乘数
Console::WriteLine(L"The sum of {0} and {1} divided by {2} is {3}",len1,len2,factor,(len1+len2)/factor);

Console::ReadKey();
return 0;
}
//-------------------------------------------
我用VS2005(打上了SP1)中的CLR环境编译上面的书籍例程,通过;但是运行时到:
Console::WriteLine(L"{1} times {0} is {2]",factor,len2,len2*factor); //double型做为乘数
时,提示说:
未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中。
其他信息: 输入字符串的格式不正确。
但我把这句注解掉后,编译通过,运行时到它的下一句:
Console::WriteLine(L"The sum of {0} and {1} divided by {2} is {3}",len1,len2,factor,(len1+len2)/factor);
再一次提示说:
未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中。
其他信息: 输入字符串的格式不正确。
我在把它也注解掉,编译通过,运行时到它的下一句:
Console::ReadKey();
再一次提示说:
未处理的“System.FormatException”类型的异常出现在 mscorlib.dll 中。
其他信息: 输入字符串的格式不正确。
真的搞不清楚了!
请高手指教!

解决方案 »

  1.   

    应该是大括号,打成一半大括号,一半中括号了:Console::WriteLine(L"{0} times {1} is {2]",factor,len2,factor*len2); //double型做为被乘数 
    Console::WriteLine(L"{1} times {0} is {2]",factor,len2,len2*factor); //double型做为乘数 
    Console::WriteLine(L"The sum of {0} and {1} divided by {2} is {3}",len1,len2,factor,(len1+len2)/factor);