2013-04-25 64 views
0

我正在尝试对我昨天在前一篇文章的帮助下创建的棒球投手矢量进行插入排序。我想按照ERA1的升序排列投手。我已经得到插入排序在过去为一组整数工作。我认为我的代码中有插入排序的语法错误。直到试图添加插入排序此程序运行良好。我得到一个错误 - 预期在[令牌之前的不合格id。预先感谢您的帮助。C++ Insertion排序一个向量

#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; 
} 
+2

头缩进你的代码张贴 – 2013-04-25 17:25:41

+0

尝试删除从'Pitcher.GetERA1([firstOutOfOrder])'括号。 – Detheroc 2013-04-25 17:26:17

+0

请包括**完整的错误信息**。它提到你的代码的哪一行有错误。 – 2013-04-25 17:27:55

回答

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不是Pitcher类的responsibility

0

除非这是功课,你最好使用排序构建从

<algorithm>