没有重载函数的实例与参数列表 C++ 匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7801431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
no instance of overloaded function matches argument list C++
提问by user1000227
I just have a very basic class that gives with functions that return the winning team of a match.
我只有一个非常基本的类,它提供了返回比赛获胜球队的函数。
here's team.cpp
这是team.cpp
class teams
{
string teamName;
string teamName2;
int score;
int score2;
public:
teams ();
void set_team1();
void set_team2();
string get_score()
{
if (score > score2)
{
return teamName;
}
else
{
return teamName2;
}
}
private:
void teams::set_team1(string teamName, int score)
{
this->teamName=teamName;
this->score=score;
}
void teams::set_team2(string teamName2, int score2)
{
this->teamName2=teamName2;
this->score2=score2;
}
};
and here's is the line where i'm getting the error in the main method. I'm trying to create a teams object.
这是我在 main 方法中遇到错误的那一行。我正在尝试创建一个团队对象。
firstTeam.set_team1(teamName, score);
firstTeam.set_team2(teamName2, score2);
Visual studio comes up and says "error: no instance of overloaded function "team::set_team1" matches the argument list".
Visual Studio 出现并说“错误:没有重载函数“team::set_team1”的实例与参数列表匹配”。
What am I missing?
我错过了什么?
This is the exact error I get:
这是我得到的确切错误:
1>c:\users\lab8.cpp(31): error C2664: 'void teams::set_team1(std::string,int)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::string'
1> with
1> [
1> _Ty=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\lab8.cpp(32): error C2664: 'void teams::set_team2(std::string,int)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::string'
1> with
1> [
1> _Ty=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> Generating Code...
1>
1>Build FAILED.
回答by Mahesh
error C2664: 'void teams::set_team1(std::string,int)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::string'
错误 C2664:“void team::set_team1(std::string,int)”:无法将参数 1 从“std::vector<_Ty>”转换为“std::string”
From the error message, it is clear that first parameter isn't of type std::string
. It is actually a std::vector
. So,
从错误消息中,很明显第一个参数不是 type std::string
。它实际上是一个std::vector
. 所以,
firstTeam.set_team1(teamName, score); // Check what is the type of teamName
If you can see that teamName
is actually a std::string
, then check whether you are compiling the right file. Save the file and try again because the code you posted and the error message has no relation.
如果您可以看到它teamName
实际上是一个std::string
,则检查您是否正在编译正确的文件。保存文件并重试,因为您发布的代码与错误消息无关。
Compiler don't provide default constructor( constructor with no arguments ) in case your class overloads the constructor.
编译器不提供默认构造函数(没有参数的构造函数),以防您的类重载构造函数。
class teams
{
string teamName;
string teamName2;
int score;
int score2;
// ...
public:
teams( string t, int s ) : teamName(x), score(s)
// Initializer list
{}
};
But the I don't understand, why you have teamName2
, score2
members as members of teams. What if there are 10 teams? Just have an instance for each team and compare them with other instances of teams.
但我不明白,为什么你有teamName2
,score2
作为团队成员的成员。如果有 10 个团队呢?只需为每个团队创建一个实例,并将它们与其他团队实例进行比较。