简体   繁体   中英

no instance of overloaded function matches argument list C++

I just have a very basic class that gives with functions that return the winning team of a match.

here's 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.

    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".

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.

error C2664: 'void teams::set_team1(std::string,int)' : cannot convert parameter 1 from 'std::vector<_Ty>' to '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,

 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.


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.

You have declared the two methods without parameters. Convert:

void set_team1();
void set_team2();

into:

void set_team1(string, int);
void set_team2(string, int);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM