简单的问题,用类创建一个对象error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-n

首先定义一个类:

class Complex
{
 double real;
 double virt;
 
public:
 Complex(double r, double v)
 {
  real = r;
  virt = v;
 }
};

首先:

构造函数没有返回类型,连void也不许有!!!

如果不幸,在构造函数钱加了void,将出现错误:

  error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-name?)

然后定义对象,如下定义,

Complex num1 = new Complex(1.0,3.0);

将会出现错误:

error C2440: 'initializing' : cannot convert from 'class Complex *' to 'class Complex'

修改方法:

1)定义指针:Complex *num1 = new Complex(1.0,3.0);

2)不用new,直接构建 Complex num1(1.0,3.0);

 

好久不写C++代码,


你可能感兴趣的:(简单的问题,用类创建一个对象error C2380: type(s) preceding 'Complex' (constructor with return type, or illegal redefinition of current class-n)