问题遇到的现象和发生背景
对比Dev C++和Visual studio上的运行结果
程序在Dev C++:能运行,结果正确。(但会提示:不能把字符串常量赋值给字符指针。)
程序在Visual studio:不能运行。提示致命错误:无构造函数可以接受源类型。
按照书上的讲解,系统应该自动调用类的构造函数。
怎么才能在Visual studio上也正确运行呢?
问题相关代码,请勿粘贴截图
#include
#include<string.h>
using namespace std;
class B
{
public:
B()
{cout<<"Defult\n";}
B(char *s,double n)
{
strcpy(name,s);
b=n;
cout<<"Constructor\n";
}
~B()
{cout<<"Destructor "<<name<<endl;}
void getb(char *s,double &n)
{
strcpy(s,name);
n=b;
}
private:
char name[20];
double b;
};
int main()
{
B *pb;
char s[20];
double n;
pb=new B[3];
pb[0]=B("Wang",5.3);
pb[1]=B("Zhang",5.0);
pb[2]=B("Li",5.1);
for(int i=0;i<3;i++)
{
pb[i].getb(s,n);
cout<<s<<','<<n<<endl;
}
delete []pb;
}