0

我正在尝试对我昨天在上一篇文章的帮助下创建的棒球投手向量进行插入排序。我想按 ERA1 升序对投手进行排序。我已经让插入排序在过去适用于一组整数。我认为我的插入排序代码中有语法错误。在尝试添加插入排序之前,该程序运行良好。我收到一个错误 - 在 [ token. 提前感谢您的帮助。

#ifndef Pitcher_H
#define Pitcher_H
#include <string>
#include <vector>

using namespace std;

class Pitcher
{
private:
    string _name;
    double _ERA1;
    double _ERA2;

public:
    Pitcher();
    Pitcher(string, double, double);
    vector<Pitcher> Pitchers;
    string GetName();
    double GetERA1();
    double GetERA2();
    void InsertionSort(vector<Pitcher>&);
    ~Pitcher();

};

#endif

#include "Pitcher.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

using namespace std;

Pitcher::Pitcher()
{
}

Pitcher::~Pitcher()
{
}

string Pitcher::GetName()
{
    return _name;
}

Pitcher::Pitcher(string name, double ERA1, double ERA2)
{
    _name = name;
    _ERA1 = ERA1;
    _ERA2 = ERA2;
}

double Pitcher::GetERA1()
{
    return _ERA1;
}

double Pitcher::GetERA2()
{
    return _ERA2;
}


#include "Pitcher.h"
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>

void InsertionSort(vector<Pitcher> Pitchers&);

using namespace std;

int main()
{

    vector<Pitcher> Pitchers;

    cout  << "Pitcher" << setw(19) << "Item ERA1" << setw(13) << 
        "Item ERA2\n" << endl;

    Pitcher h2("Bob Jones", 1.32, 3.49); 
    Pitchers.push_back(h2); 
    Pitcher h3("F Mason", 7.34, 2.07); 
    Pitchers.push_back(h3); 
    Pitcher h1("RA Dice", 0.98, 6.44); 
    Pitchers.push_back(h1); 

    for(unsigned i = 0; i < Pitchers.size(); ++i)
    {
        cout << setw(19);
        cout << left << Pitchers[i].GetName() << "$" << 
            setw(10) << Pitchers[i].GetERA1() << 
            right << "$" << Pitchers[i].GetERA2() << "\n";
    }

    cout << endl;

//------------------------------------------------------

    InsertionSort(Pitchers);

//Now print the numbers
    cout<<"The numbers in the vector after the sort are:"<<endl;
    for(int i = 0; i < Pitchers.size(); i++)
    {
        cout<<Pitchers[i].GetERA1()<<" ";
    }
    cout<<endl<<endl;

    system("PAUSE");
    return 0;
}

void InsertionSort(vector<Pitcher> &Pitchers)
{
    int firstOutOfOrder = 0;
    int location = 0;
    int temp;
    int totalComparisons = 0; //debug purposes

    for(firstOutOfOrder = 1; firstOutOfOrder < Pitchers.size() ; firstOutOfOrder++)
    {
        if(Pitcher.GetERA1([firstOutOfOrder]) < Pitcher.GetERA1[firstOutOfOrder - 1])
        {
            temp = Pitcher[firstOutOfOrder];
            location = firstOutOfOrder;
            do
            {
                totalComparisons++;

                Pitcher.GetERA1[location] = Pitcher.GetERA1[location - 1];
                location--;
            }while(location > 0 && Pitcher.GetERA1[location - 1] > temp);
            Pitcher.GetERA1[location] = temp;
        }
    }
    cout<<endl<<endl<<"Comparisons: "<<totalComparisons<<endl<<endl;
}
4

2 回答 2

1

这里:

  for(firstOutOfOrder = 1; firstOutOfOrder < Pitchers.size() ; firstOutOfOrder++)
{
    if(Pitchers[firstOutOfOrder].GetERA1() < Pitchers[firstOutOfOrder-1].GetERA1())
    {    //^^^your way was not right, should first access the object then 
         //access member function
        temp = Pitcher[firstOutOfOrder];
                 //^^^should be Pitchers, similar errors below           
        location = firstOutOfOrder;
        do
        {
            totalComparisons++;

            Pitcher.GetERA1[location] = Pitcher.GetERA1[location - 1];
           //^^^similar error as inside if condition
            location--;
        }while(location > 0 && Pitcher.GetERA1[location - 1] > temp);
                             //^^^similar error as inside if condition
        Pitcher.GetERA1[location] = temp;
        //^^similar error as in if condition and name error
    }
}

同时,您将InsertionSort声明作为Pitcher类的成员

  public:
       .
       .
      void InsertionSort(vector<Pitcher>&);

你也在里面声明了相同的函数main

  void InsertionSort(vector<Pitcher> Pitchers&);
                        //should be vector<Pitcher>& Pitchers
  using namespace std;
  int main()

在您的情况下,可能应该删除成员函数。InsertionSort不是responsibility你的Pitcher班级。

于 2013-04-25T17:34:50.253 回答
0

除非这是家庭作业,否则最好使用内置排序

<algorithm>
于 2013-04-25T17:45:58.503 回答