我自己在编写时曾经写过:class1 a = new class1(); 但是会报错,提示缺少" * ",我记得在VC++中是可以这样定义的,不知道这是为什么?
我自己在编写时曾经写过:class1 a = new class1(); 但是会报错,提示缺少" * ",我记得在VC++中是可以这样定义的,不知道这是为什么?
//SavingsAccount.h
#pragma once
#using <mscorlib.dll>
using namespace System;
//银行存款小程序。
public __gc class SavingsAccount
{
private:
double savingsBalance; //储户存储的总金额。
static double annualInterestRate; //利率。
public:
SavingsAccount(double savings)
{
if(savings >= 0)
savingsBalance = savings;
else
savingsBalance = 0.0;
}
//~SavingsAccount();
__property double get_Savings()
{
savingsBalance = savingsBalance + savingsBalance * annualInterestRate / 12;
return savingsBalance;
}
__property static void set_Annual(double annual)
{
annualInterestRate = annual;
}
__property static double get_Annual()
{
return annualInterestRate;
}
void PrintSavingsBalance()
{
Console::Write(S"Savings Balance is : ");
Console::WriteLine(savingsBalance);
}
};
//SavingsAccount.cpp
// 这是使用应用程序向导生成的 VC++
// 应用程序项目的主项目文件。
#include "stdafx.h"
#include "SavingsAccount.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
SavingsAccount *save1 = new SavingsAccount(2000);
save1->set_Annual(0.04);
save1->get_Savings();
save1->PrintSavingsBalance();
SavingsAccount *save2 = new SavingsAccount(3000);
save2->set_Annual(0.05);
save2->get_Savings();
save2->PrintSavingsBalance();
return 0;
}
这样是没有错误的,但是把save1和save2前面的*去掉就会出错,请大侠们帮我解释一下,我先谢谢了。
在线等
//SavingsAccount.h
#pragma once
#using <mscorlib.dll>
using namespace System;
public class SavingsAccount
{
private:
double savingsBalance;
public:
SavingsAccount(double savings)
{
if(savings >= 0)
savingsBalance = savings;
else
savingsBalance = 0.0;
}
};
//SavingsAccount.cpp
#include "stdafx.h"
#include "SavingsAccount.h"
#using <mscorlib.dll>
using namespace System;
int _tmain()
{
SavingsAccount save1 = SavingsAccount(2000);
return 0;
}
我曾经把程序改写成这样,但是还是会报错:
e:\LianXi\SavingsAccount\SavingsAccount.h(10): error C3381: “SavingsAccount” : 程序集访问说明符只能应用于托管类型
难道在Visual C++.NET中只能将新的对象定义成指针型?