【问题标题】:no instance of overloaded function matches argument list C++没有重载函数的实例匹配参数列表 C++
【发布时间】:2011-10-18 01:17:56
【问题描述】:

我只有一个非常基础的类,它提供了返回比赛获胜球队的功能。

这里是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;
}
};    

这是我在 main 方法中遇到错误的那一行。我正在尝试创建一个团队对象。

    firstTeam.set_team1(teamName, score);
    firstTeam.set_team2(teamName2, score2);

Visual Studio 出现并说“错误:没有重载函数的实例“team::set_team1”与参数列表匹配”。

我错过了什么?

这是我得到的确切错误:

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.

【问题讨论】:

  • 欢迎来到 Stack Overflow!请提供一个完整的、最小的程序来演示您的问题。摆脱所有没有损坏的东西,然后复制粘贴剩下的东西。请参阅sscce.org 了解更多信息。
  • 你能给出来自 Visual Studio 的确切信息吗?这看起来像家庭作业。
  • 是的,它的功课,但我只是在特定问题上寻求帮助。我在原始帖子中发布了错误消息。

标签: c++ visual-studio-2010


【解决方案1】:

错误 C2664: 'void teams::set_team1(std::string,int)' : 无法将参数 1 从 'std::vector<_ty>' 转换为 'std::string'

从错误消息中,很明显第一个参数不是std::string 类型。它实际上是一个std::vector。所以,

 firstTeam.set_team1(teamName, score);  // Check what is the type of teamName

如果你可以看到teamName实际上是一个std::string,那么请检查你是否正在编译正确的文件。保存文件并重试,因为您发布的代码与错误消息无关。


编译器不提供默认构造函数(没有参数的构造函数),以防你的类重载构造函数。

class teams
{
    string teamName;
    string teamName2;
    int score;
    int score2;

    // ...

    public:
       teams( string t, int s ) : teamName(x), score(s)
                               // Initializer list
       {}
};

但我不明白,为什么你有teamName2score2 成员作为团队成员。如果有 10 个团队呢?只需为每个团队创建一个实例,然后将它们与其他团队实例进行比较。

【讨论】:

  • 谢谢。这就是问题所在。 teamName 和 score 都是向量。
  • 现在我得到这个:1>lab8.obj : error LNK2019: unresolved external symbol "public: __thiscall teams::teams(void)" (??0teams@@QAE@XZ) 在函数中引用_main 1>C:\Users\samuelrm\Desktop\lab8\lab8\lab8\Debug\lab8.exe : 致命错误 LNK1120: 1 unresolved externals" 任何想法这是什么意思?
  • std::vector&lt;std::string&gt;std::string 的类型不同。
  • 您忘记提供默认构造函数的定义。您刚刚提供了构造函数的声明(teams::teams())作为类声明的一部分,链接器在类实例化时找不到构造函数的定义,链接器抱怨。
  • 我认为任何类的默认构造函数都是 team();所以我必须有类似团队的东西(字符串 teamName,int score)对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2013-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-12
相关资源
最近更新 更多