且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误:转换为非标量类型

更新时间:2021-09-28 18:08:43

code>商业bs1(1,businessName1); 。如果要使用 = ,您还可以使用复制初始化 Business bs2 = Business(2,businessName2);

The proper syntax would be Business bs1(1,businessName1);. If you want to use =, you can also use copy intialization Business bs2 = Business(2,businessName2);.

前者称为直接初始化。他们是不一样的东西,但

The former is known as direct initialization. They aren't exactly the same thing though, see Is there a difference in C++ between copy initialization and direct initialization? for in-depth information.

商务bs1中,在初始化和直接初始化之间的C ++中有区别吗? =(1,businessName1); 1 和数组 businessName1 a href =http://en.wikipedia.org/wiki/Comma_o​​perator>逗号运算符。逗号运算符计算第一个操作数,即 1 并丢弃结果,并返回第二个操作数的值,这是一个数组。换句话说,你的代码相当于 Business bs1 = businessName1; 。这是为什么错误消息说它不能将 char [10] 转换为 Business 对象。

In Business bs1 = (1,businessName1); the 1 and array businessName1 are separated by the comma operator. The comma operator evaluates the first operand, i.e. 1 and throws away the results and returns the value of the second operand, which is an array in your case. In other words, your code is the equivalent of Business bs1 = businessName1;. This is why the error message says it cannot convert a char[10] to a Business object.