当传递整数文字 0 时,调用采用 unsigned int 或指针的重载函数是不明确的

发布于 2024-10-11 12:51:49 字数 543 浏览 8 评论 0原文

这个错误信息是什么意思?

error: call of overloaded ‘setval(int)’ is ambiguous
huge.cpp:18: note: candidates are: void huge::setval(unsigned int)
huge.cpp:28: note:                 void huge::setval(const char*)

我的代码如下所示:

class huge {
private:
    unsigned char data[8];
public:
    void setval(unsigned int) { /* .... */ }
    void setval(const char *) { /* ... */ }
};

int main() {
    huge p;
    p.setval(0);
}

What does this error message mean?

error: call of overloaded ‘setval(int)’ is ambiguous
huge.cpp:18: note: candidates are: void huge::setval(unsigned int)
huge.cpp:28: note:                 void huge::setval(const char*)

My code looks like this:

class huge {
private:
    unsigned char data[8];
public:
    void setval(unsigned int) { /* .... */ }
    void setval(const char *) { /* ... */ }
};

int main() {
    huge p;
    p.setval(0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

软糖 2024-10-18 12:51:49

文字 0 在 C++ 中有两个含义。
一方面,它是一个值为 0 的整数。
另一方面,它是一个空指针常量。

由于您的 setval 函数可以接受 intchar*,因此编译器无法决定您指的是哪个重载。

最简单的解决方案是将 0 转换为正确的类型。
另一种选择是确保首选 int 重载,例如将另一个重载设置为模板:

class huge
{
 private:
  unsigned char data[BYTES];
 public:
  void setval(unsigned int);
  template <class T> void setval(const T *); // not implemented
  template <> void setval(const char*);
};

The literal 0 has two meanings in C++.
On the one hand, it is an integer with the value 0.
On the other hand, it is a null-pointer constant.

As your setval function can accept either an int or a char*, the compiler can not decide which overload you meant.

The easiest solution is to just cast the 0 to the right type.
Another option is to ensure the int overload is preferred, for example by making the other one a template:

class huge
{
 private:
  unsigned char data[BYTES];
 public:
  void setval(unsigned int);
  template <class T> void setval(const T *); // not implemented
  template <> void setval(const char*);
};
2024-10-18 12:51:49

如果我们考虑常量值的类型,解决方案非常简单,应该是“unsigned int”而不是“int”。

而不是:

setval(0)

使用:

setval(0u)

后缀“u”告诉编译器这是一个无符号整数。那么,就不需要任何转换,并且调用将是明确的。

The solution is very simple if we consider the type of the constant value, which should be "unsigned int" instead of "int".

Instead of:

setval(0)

Use:

setval(0u)

The suffix "u" tell the compiler this is a unsigned integer. Then, no conversion would be needed, and the call will be unambiguous.

殊姿 2024-10-18 12:51:49

p.setval(0); 替换为以下内容。

const unsigned int param = 0;
p.setval(param);

这样它就可以确定常量 0 是什么类型。

replace p.setval(0); with the following.

const unsigned int param = 0;
p.setval(param);

That way it knows for sure which type the constant 0 is.

追风人 2024-10-18 12:51:49

使用

p.setval(static_cast<const char *>(0));

or

p.setval(static_cast<unsigned int>(0));

正如错误所示,0 的类型是 int。这可以很容易地转换为 unsigned intconst char *。通过手动进行强制转换,您可以告诉编译器您想要哪个重载。

Use

p.setval(static_cast<const char *>(0));

or

p.setval(static_cast<unsigned int>(0));

As indicated by the error, the type of 0 is int. This can just as easily be cast to an unsigned int or a const char *. By making the cast manually, you are telling the compiler which overload you want.

2024-10-18 12:51:49

转换该值,以便编译器知道要调用哪个函数:

p.setval(static_cast<const char *>( 0 ));

请注意,在编译代码后,代码中存在分段错误(取决于您真正想要调用的函数)。

Cast the value so the compiler knows which function to call:

p.setval(static_cast<const char *>( 0 ));

Note, that you have a segmentation fault in your code after you get it to compile (depending on which function you really wanted to call).

望喜 2024-10-18 12:51:49

这是不明确的,因为指针只是一个地址,因此 int 也可以被视为指针 - 0(int)可以转换为 unsigned intchar * 同样容易。

简短的答案是使用明确是其实现类型之一的内容来调用 p.setval() :unsigned intchar *p.setval(0U)p.setval((unsigned int)0)p.setval((char *)0)都会编译。

不过,首先避免这种情况的发生通常是一个好主意,不要定义具有此类相似类型的重载函数。

That is ambiguous because a pointer is just an address, so an int can also be treated as a pointer – 0 (an int) can be converted to unsigned int or char * equally easily.

The short answer is to call p.setval() with something that's unambiguously one of the types it's implemented for: unsigned int or char *. p.setval(0U), p.setval((unsigned int)0), and p.setval((char *)0) will all compile.

It's generally a good idea to stay out of this situation in the first place, though, by not defining overloaded functions with such similar types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文