简体   繁体   English

在C ++中返回错误的类型

[英]Return the wrong type in C++

I have a class Color that contains three float components (r, g, b). 我有一个Color类,它包含三个float组件(r,g,b)。

I have to program the following function : 我必须编写以下函数:

Color getColor(unsigned char values[], int i) Color getColor(unsigned char values [],int i)

Normally I should program it like this : 通常我应该像这样编程:

Color getColor(unsigned char values[], int i){
     return Color((float) values[i]/255.0, (float) values[i+1]/255.0, (float) values[i+2]/255.0);
}

But by error I did 但是我做错了

return values[i];

When I compiled I didn't get any compilation error and I haven't get run time error either. 当我编译时,我没有得到任何编译错误,我也没有得到运行时错误。

Why this is possible? 为什么这是可能的?

This could be a result of non-explicit constructor of Color class accepting unsigned char as its argument. 这可能是Color类的非显式构造函数接受unsigned char作为其参数的结果。

That means you have constructor in Color class with single argument OR multiple arguments rest being default parameters. 这意味着你在Color类中有一个带有单个参数的构造函数,或者多个参数仍然是默认参数。

检查Color类中的构造函数,并用所需的构造函数覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM