" double(*)(string,double,double)" to" double"尝试传递函数C ++时的转换错误

时间:2018-03-13 16:41:37

标签: c++

我对c ++很新,我正在编写一个有结构的程序,以及3个名为win_percentage,Wins_losses和winning_teams的函数。这项任务的目的是创建一个程序,打印出13个学校团队,他们的记录,然后打印出胜利超过.500的团队。

我不断收到错误消息:

  

无法转换' double(*)(std :: __ cxx11 :: string,double,double){aka double(*)(std :: __ cxx11 :: basic_string,double,double)}& #39;至   '双'争论#4;' to' void wins_losses(std :: __ cxx11 :: string,   双,双,双)'
  wins_losses(school1.name,school1.wins,school1.losses,   win_percentage);

当我尝试将win_percentage函数传递给其他函数时,会出现问题。我怎么能解决这个问题?请帮助lol

这是我的代码。 (ps我将为这个程序制作一个for循环)

#include <iostream>
#include<string>
using namespace std;

struct Team 
{
  string name;
  int wins;
  int losses;
};

double win_percentage (string , double , double );
void wins_losses (string , double , double , double );
void winning_teams (string , double , double , double );

int main() {
  Team school1 = {"Bethune-Cookman", 11, 3};
  Team school2 = {"Coppin State", 5, 9};
  Team school3 = {"Delaware State", 1, 13};
  Team school4 = {"Florida A&M", 6, 8};
  Team school5 = {"Hampton", 11, 4};
  Team school6 = {"Howard", 6, 8};
  Team school7 = {"Maryland Eastern Shore", 2, 12};
  Team school8 = {"Morgan State", 6, 8};
  Team school9 = {"North Carolina A&T", 10, 4};
  Team school10 = {"North Carolina Central", 8, 6};
  Team school11 = {"Norfolk State", 10, 4};
  Team school12 = {"Savannah State", 10, 4};
  Team school13 = {"South Carolina State", 6, 8};




  cout<< "MEAC MENS BASKETBALL TEAMS CONFERENCE STANDINGS"<< endl;

  cout<< school1.name<<" "<<school1.wins<<"-"<<school1.losses<< endl;
  cout<< school2.name<<" "<<school2.wins<<"-"<<school2.losses<< endl;
  cout<< school3.name<<" "<<school3.wins<<"-"<<school3.losses<< endl;
  cout<< school4.name<<" "<<school4.wins<<"-"<<school4.losses<< endl;
  cout<< school5.name<<" "<<school5.wins<<"-"<<school5.losses<< endl;
  cout<< school6.name<<" "<<school6.wins<<"-"<<school6.losses<< endl;
  cout<< school7.name<<" "<<school7.wins<<"-"<<school7.losses<< endl;
  cout<< school8.name<<" "<<school8.wins<<"-"<<school8.losses<< endl;
  cout<< school9.name<<" "<<school9.wins<<"-"<<school9.losses<< endl;
  cout<< school10.name<<" "<<school10.wins<<"-"<<school10.losses<< endl;
  cout<< school11.name<<" "<<school11.wins<<"-"<<school11.losses<< endl;
  cout<< school12.name<<" "<<school12.wins<<"-"<<school12.losses<< endl;
  cout<< school13.name<<" "<<school13.wins<<"-"<<school13.losses<< endl;




  cout<<"TEAMS WINNING PERCENTAGE"<<endl;
  win_percentage(school1.name,school1.wins,school1.losses);
  win_percentage(school2.name,school2.wins,school2.losses);
  win_percentage(school3.name,school3.wins,school3.losses);
  win_percentage(school4.name,school4.wins,school4.losses);
  win_percentage(school5.name,school5.wins,school5.losses);
  win_percentage(school6.name,school6.wins,school6.losses);
  win_percentage(school7.name,school7.wins,school7.losses);
  win_percentage(school8.name,school8.wins,school8.losses);
  win_percentage(school9.name,school9.wins,school9.losses);
  win_percentage(school10.name,school10.wins,school10.losses);
  win_percentage(school11.name,school11.wins,school11.losses);
  win_percentage(school12.name,school12.wins,school12.losses);
  win_percentage(school13.name,school13.wins,school13.losses);
  cout<<"TEAMS WITH WINNING RECORDS"<<endl;

  wins_losses(school1.name,school1.wins,school1.losses, win_percentage);
  wins_losses(school2.name,school2.wins,school2.losses,win_percentage);
  wins_losses(school3.name,school3.wins,school3.losses,win_percentage);
  wins_losses(school4.name,school4.wins,school4.losses,win_percentage);
  wins_losses(school5.name,school5.wins,school5.losses,win_percentage);
  wins_losses(school6.name,school6.wins,school6.losses,win_percentage);
  wins_losses(school7.name,school7.wins,school7.losses,win_percentage);
  wins_losses(school8.name,school8.wins,school8.losses,win_percentage);
  wins_losses(school9.name,school9.wins,school9.losses,win_percentage);
  wins_losses(school10.name,school10.wins,school10.losses,win_percentage);
  wins_losses(school11.name,school11.wins,school11.losses,win_percentage);
  wins_losses(school12.name,school12.wins,school12.losses,win_percentage);
  wins_losses(school13.name,school13.wins,school13.losses,win_percentage);

  winning_teams(school1.name,school1.wins,school1.losses,win_percentage);
  winning_teams(school2.name,school2.wins,school2.losses,win_percentage);
  winning_teams(school3.name,school3.wins,school3.losses,win_percentage);
  winning_teams(school4.name,school4.wins,school4.losses,win_percentage);
  winning_teams(school5.name,school5.wins,school5.losses,win_percentage);
  winning_teams(school6.name,school6.wins,school6.losses,win_percentage);
  winning_teams(school7.name,school7.wins,school7.losses,win_percentage);
  winning_teams(school8.name,school8.wins,school8.losses,win_percentage);
  winning_teams(school9.name,school9.wins,school9.losses,win_percentage);
  winning_teams(school10.name,school10.wins,school10.losses,win_percentage);
  winning_teams(school11.name,school11.wins,school11.losses,win_percentage);
  winning_teams(school12.name,school12.wins,school12.losses,win_percentage);
  winning_teams(school13.name,school13.wins,school13.losses,win_percentage);

  return 0;
}

double win_percentage(string a, double b, double c){
  double x;
  x == b / (b + c);
  cout<< a<<" "<<x<<endl;

}
void wins_losses( string e, double f, double g, double h){

   cout<< e<< "Record is"<<f<<"-"<<g<< h<<endl;
}
void winning_teams(string w, int f, int y, double z){
  if ( z >.500){
    cout<< w<<" "<<f<< "-"<<y<< " "<< z<< endl;

  }
}

3 个答案:

答案 0 :(得分:3)

我注意到这里有一些问题。第一:

double win_percentage(string a, double b, double c){
  double x;
  x == b / (b + c);
  cout<< a<<" "<<x<<endl;

}

该功能没有:

  1. 分配给变量x,因为它使用了相等运算符。
  2. 返回一个值,因此该函数的调用者无法访问x。这是undefined behavior,因为非空函数没有返回。
  3. 下一步:

    winning_teams(school1.name,school1.wins,school1.losses,win_percentage);
    

    此调用尝试将函数指针传递给期望double的函数。相反,使用函数指针语法甚至更好,使用std::function

    但是,我怀疑你确实需要在这里传递一个函数。如果要传递函数的结果,请执行:

    winning_teams(school1.name,
        school1.wins,
        school1.losses,
        win_percentage(/* Parameters to the function */));
    

答案 1 :(得分:1)

当您致电wins_losses时,您将win_percentage作为参数传递:

wins_losses(school1.name,school1.wins,school1.losses, win_percentage);

函数wins_losses需要double,但您传递对函数的引用,即win_percentage

这就是原因。

因此,要么调用win_percentage,使其返回值用作参数,或者传递一个double值:

wins_losses(school1.name,school1.wins,school1.losses, win_percentage(school1.name,school1.wins,school1.losses));

wins_losses(school1.name,school1.wins,school1.losses,5.0);

答案 2 :(得分:0)

您正在使用比较运算符而不是win_percentage

中的赋值运算符
double win_percentage(string a, double b, double c){
      double x;
      x == b / (b + c);
      cout<< a<<" "<<x<<endl;

    }

尝试使用: x =(b /(b + c))