-1

以下是我当前代码的 3 部分。当我尝试遍历我的向量并取消对“比较”方法的引用时,我的 main.cpp 文件中出现错误。有人可以帮我找出导致此错误的原因吗?

主要.cpp:

#include <iostream>
#include <vector>
#include "Scores.h"

using namespace std;

int main()
{
    vector<comparable*> comparables;

    for(int i = 0; i < 5; i++)
    {
        comparables.push_back(new Player());
    }

    for(vector<comparable*>::iterator itr = comparables.begin(), end = comparables.end(); itr != end ; itr++ )
    {
        (itr*)->compare(); ****THIS IS THE LINE WHERE THE ERROR OCCURS****************
    }

    cout << "Mission Accomplished!\n\n";

    return 0;
}

我收到以下错误:错误:')' 标记之前的预期主表达式。我想不通。顺便说一句,这就是更改后的代码的样子。

分数.cpp:

#include <iostream>
#include "Scores.h"
#include <string>

using namespace std;

void Player::compare()
{
    cout << "comparing" << '\n';
}

Player::Player()
{
   getname();
   getscore();
}

void Player::getscore()
{
    cout << "Enter score: ";
    cin >> player_score;
}

void Player::getname()
{
    cout << "Enter Name: ";
    cin >> player_name;
}

头文件 (Scores.h)

#ifndef SCORES_H_INCLUDED
#define SCORES_H_INCLUDED

class comparable
{
    public:

    virtual void compare() = 0;

};

class Player: public comparable
{
    public:

    Player();
    void compare();
    void getscore();
    void getname();

    private:
    std::string player_name;
    int player_score;
};


#endif // SCORES_H_INCLUDED
4

1 回答 1

0

换了怎么办

for(vector<comparable*>::iterator itr = comparables.begin(), end = comparables.end(); itr != end ; itr++ )
{
    (itr*)->compare(); ****THIS IS THE LINE WHERE THE ERROR OCCURS****************
}

经过

for(vector<comparable*>::iterator itr = comparables.begin(), end = comparables.end(); itr != end ; itr++ )
{
    (*itr)->compare(); // THIS WAS THE LINE WHERE THE ERROR USED TO OCCUR :)
}
于 2013-08-07T12:20:26.933 回答