c++ - 对程序的输出感到困惑
问题描述
我是 C++ 新手,我正在练习上课。请有人帮我解决这个问题。有什么错误?
class Test1 {
int y;
};
class Test2 {
int x;
Test1 t1;
public:
operator Test1() { return t1; }
operator int() { return x; }
};
void fun ( int x) { };
void fun ( Test1 t ) { };
int main() {
Test2 t;
fun(t);
return 0;
}
解决方案
编译时:
t.cc: In function ‘int main()’: t.cc:18:10: error: call of overloaded ‘fun(Test2&)’ is ambiguous fun(t); ^ t.cc:13:6: note: candidate: void fun(int) void fun ( int x) { } ^~~ t.cc:14:6: note: candidate: void fun(Test1) void fun ( Test1 t ) { } ^~~
要解决这个问题,您必须表明您的选择:
fun(static_cast<Test1>(t));
或者
fun(static_cast<int>(t));
或者当然要删除转换运算符之一
推荐阅读
- vba - CommandBarControl 在单击按钮之前执行 .OnAction
- javascript - 为什么当我使用 import-export 时 html 不运行我导入的 javascript
- android - 解析平台在Android中使用join
- java - java -version 在 `jre1.8/lib/i386/server/libjvm.so' 返回缺少的 `server' JVM
- kubernetes - 如何连接在 k8s 集群中使用 helm 图表创建的 mongodb 副本集
- android - 如何在 Firebase 分析控制台上显示具有事件计数的用户属性值列表
- c# - 在构造函数调用中设置属性的值如何与 JsonProperty 一起使用?
- c# - 这两种使用foreach的方式有区别吗?
- coq - 如何消除表达式内部的析取?
- c# - 实体框架创建与现有实体相关的新实体