Compilation error: More than one instance of overloaded function matches the arument list
Compilation error: More than one instance of overloaded function matches the arument list
我有一份学校作业:
i. Create a classical Guitar object with price 0 and type = “classical”. Set the new price to 0 and display all the information about the Guitar object.
ii. Create an electric Guitar object with price 5 and type = “electric”. Change the price as there is a promotion and display all the information about the Guitar object.
我正在尝试自己解决它,但我是 C++ 的新手,并且遇到了我无法理解的编译器错误。
这里是我在 Guitar.h 文件中创建的 class。
#pragma once
#include<iostream>
#include <string>
#include<sstream>
using namespace std;
class Guitar
{
private:
string type;
double price;
public:
Guitar(string type, double price);
string getType();
double getPrice();
void setPrice(double newPrice);
void setPrice(bool promotion);
string toString();
};
这是我的 Guitar.cpp 文件中的 class 实现
#include "Guitar.h"
Guitar::Guitar(string typeclass, double priceclass)
{
type = typeclass;
price = priceclass;
}
string Guitar::getType()
{
return type;
}
double Guitar::getPrice()
{
return price;
}
void Guitar::setPrice(double newPriceclass)
{
price = newPriceclass;
}
void Guitar::setPrice(bool promotion)
{
if (promotion == true)
price *= 0.9;
}
string Guitar::toString()
{
stringstream info;
info << "Guitar Type: " << type << endl
<< "Price: " << price << endl;
return info.str();
}
终于有了主文件 GuitarApp.cpp
#include"Guitar.h"
int main()
{
Guitar guitar1("Classical", 150.0);
guitar1.setPrice(100) << endl;
cout << guitar1.toString() << endl;
Guitar guitar2("Electrical", 135.0);
guitar2.setPrice(true);
cout << guitar2.toString() << endl;
}
我有 2 个错误:
- more than one instance of overloaded function
Guitar::setPrice
matches the argument list
Guitar::setPrice
ambiguous call to overloaded function.
有人可以向我解释错误以及我应该如何编译代码吗?
编辑: 将 100
更改为 100.0
后,我又遇到了 4 个错误:
- mismatch in formal parameter list
- expression must have integral or unscoped enum type
- cannot determine which instance of function template
std::endl
; is intended
- '<<': unable to resolve function overload
所有错误都在我的 GuitarApp.cpp 的第 7 行,即
guitar1.setprice(100.0)<<endl;
如果我将吉他的价格从 100.0
编辑回 100
,我会得到我最初遇到的两个错误。
文字 100
的类型是 int
。由于 int
可以很容易地转换为 bool
和转换为 double
,因此不清楚应该调用哪些函数。
将 100
更改为 100.0
(double
文字)应该可以解决此问题。
通常我们不在这里修理家庭作业。还不如自己去问"Why this line of code did not work",而不是把一堆作业扔在这里,指望别人准备好...
另请记住,您的问题是 "Off topic",因为:
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.
好的,您的代码存在以下语法错误:
guitar1.setPrice(100.) ; // see the "." behind the number!
你必须方法:
void setPrice(double newPrice);
void setPrice(bool promotion);
你写道:
guitar1.setPrice(100)
100
是 int
而不是 double
而不是 bool
。所以编译器不能决定从你的 100 中生成一个 bool
值 true
或使它成为一个 double
值 100.
。因此,只需添加一个点即可使您的值成为编译器将其视为 double
.
的浮点数
下一个错误:
cout << guitar2.toString() << endl; // see the "2" behind guitar !
只有一个错字...
一些备注:
在头文件和源文件中拆分这样的 class 是错误的!优化器没有机会内联函数。
使用using namespace std;
可能会很糟糕!最好写 std::string 和所有你需要看到你的定义来自哪个命名空间。这会增加一些工作量,但稍后阅读会更好,尤其是当您使用来自多个库的多个名称空间时。
解释:
第一次查看时很容易省去输入一些字符。但是,如果您稍后(重新)在一个更大的应用程序中使用您的代码,在该应用程序中您必须处理许多可能定义 functions/classes/what 的库,这些库可能与其他库中的一个具有相同的名称,那么您就开始更改您的代码。
一个简单的例子是让 posix read 和 istream read 到位。在这里,给 select 一个未绑定到命名空间的 posix 的 '::read' 也是一个好主意。
提示不要使用 using namespace
是教条式的吗?我的个人经验很简单,如果您使用它,如果您的代码稍后在更大的应用程序中(重新)使用,您可能 运行 会遇到问题。对于 me 来说,必须这样编写我的代码,can 可以毫无问题地(重新)使用和/或很多 adoptions/corrections 将来。
你必须做出决定:今天留一些字符来打字,以后 运行 可能会遇到麻烦,或者现在就开始工作。
也许家庭作业代码可以这样做。但是我觉得讨论一下这种代码编写可能出现的问题是一个很好的观点。
我有一份学校作业:
i. Create a classical Guitar object with price 0 and type = “classical”. Set the new price to 0 and display all the information about the Guitar object.
ii. Create an electric Guitar object with price 5 and type = “electric”. Change the price as there is a promotion and display all the information about the Guitar object.
我正在尝试自己解决它,但我是 C++ 的新手,并且遇到了我无法理解的编译器错误。
这里是我在 Guitar.h 文件中创建的 class。
#pragma once
#include<iostream>
#include <string>
#include<sstream>
using namespace std;
class Guitar
{
private:
string type;
double price;
public:
Guitar(string type, double price);
string getType();
double getPrice();
void setPrice(double newPrice);
void setPrice(bool promotion);
string toString();
};
这是我的 Guitar.cpp 文件中的 class 实现
#include "Guitar.h"
Guitar::Guitar(string typeclass, double priceclass)
{
type = typeclass;
price = priceclass;
}
string Guitar::getType()
{
return type;
}
double Guitar::getPrice()
{
return price;
}
void Guitar::setPrice(double newPriceclass)
{
price = newPriceclass;
}
void Guitar::setPrice(bool promotion)
{
if (promotion == true)
price *= 0.9;
}
string Guitar::toString()
{
stringstream info;
info << "Guitar Type: " << type << endl
<< "Price: " << price << endl;
return info.str();
}
终于有了主文件 GuitarApp.cpp
#include"Guitar.h"
int main()
{
Guitar guitar1("Classical", 150.0);
guitar1.setPrice(100) << endl;
cout << guitar1.toString() << endl;
Guitar guitar2("Electrical", 135.0);
guitar2.setPrice(true);
cout << guitar2.toString() << endl;
}
我有 2 个错误:
- more than one instance of overloaded function
Guitar::setPrice
matches the argument listGuitar::setPrice
ambiguous call to overloaded function.
有人可以向我解释错误以及我应该如何编译代码吗?
编辑: 将 100
更改为 100.0
后,我又遇到了 4 个错误:
- mismatch in formal parameter list
- expression must have integral or unscoped enum type
- cannot determine which instance of function template
std::endl
; is intended- '<<': unable to resolve function overload
所有错误都在我的 GuitarApp.cpp 的第 7 行,即
guitar1.setprice(100.0)<<endl;
如果我将吉他的价格从 100.0
编辑回 100
,我会得到我最初遇到的两个错误。
文字 100
的类型是 int
。由于 int
可以很容易地转换为 bool
和转换为 double
,因此不清楚应该调用哪些函数。
将 100
更改为 100.0
(double
文字)应该可以解决此问题。
通常我们不在这里修理家庭作业。还不如自己去问"Why this line of code did not work",而不是把一堆作业扔在这里,指望别人准备好...
另请记住,您的问题是 "Off topic",因为:
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers.
好的,您的代码存在以下语法错误:
guitar1.setPrice(100.) ; // see the "." behind the number!
你必须方法:
void setPrice(double newPrice);
void setPrice(bool promotion);
你写道:
guitar1.setPrice(100)
100
是 int
而不是 double
而不是 bool
。所以编译器不能决定从你的 100 中生成一个 bool
值 true
或使它成为一个 double
值 100.
。因此,只需添加一个点即可使您的值成为编译器将其视为 double
.
下一个错误:
cout << guitar2.toString() << endl; // see the "2" behind guitar !
只有一个错字...
一些备注:
在头文件和源文件中拆分这样的 class 是错误的!优化器没有机会内联函数。
使用using namespace std;
可能会很糟糕!最好写 std::string 和所有你需要看到你的定义来自哪个命名空间。这会增加一些工作量,但稍后阅读会更好,尤其是当您使用来自多个库的多个名称空间时。
解释:
第一次查看时很容易省去输入一些字符。但是,如果您稍后(重新)在一个更大的应用程序中使用您的代码,在该应用程序中您必须处理许多可能定义 functions/classes/what 的库,这些库可能与其他库中的一个具有相同的名称,那么您就开始更改您的代码。
一个简单的例子是让 posix read 和 istream read 到位。在这里,给 select 一个未绑定到命名空间的 posix 的 '::read' 也是一个好主意。
提示不要使用 using namespace
是教条式的吗?我的个人经验很简单,如果您使用它,如果您的代码稍后在更大的应用程序中(重新)使用,您可能 运行 会遇到问题。对于 me 来说,必须这样编写我的代码,can 可以毫无问题地(重新)使用和/或很多 adoptions/corrections 将来。
你必须做出决定:今天留一些字符来打字,以后 运行 可能会遇到麻烦,或者现在就开始工作。
也许家庭作业代码可以这样做。但是我觉得讨论一下这种代码编写可能出现的问题是一个很好的观点。