请求成员的数组时收到错误

时间:2013-07-21 22:22:21

标签: c++ arrays

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int winnings[4] = {0,0,0,0};
int* p_winnings = winnings;

void CalculateGame(int* p_games);

int main()
{
    int games[6] = {12,13,14,23,24,34};
    int* p_games = games;
    int favorite_team,games_played,team1,team2,score1,score2,i;
    ifstream data("DATA11.txt");
    data >> favorite_team >> games_played;
    vector<int> standings(4);
    for (i = 0;i < 4; i++)
        standings[i] = 0;
    for (i = 0;i < games_played;i++)
    {
        data >> team1 >> team2 >> score1 >> score2;
        if (score1 > score2)
            standings[team1 - 1] += 3;
        if (score1 == score2)
            standings[team1-1]++,standings[team2-1]++;
        if (score1 < score2)
            standings[team2-1] += 3;
        for (i = 0; i < games.size();i++)
        {
            int temp1,temp2,temp;
            temp = games[i];
            temp2 = temp % 10;
            temp /= 10;
            if (temp2 == team2 && temp == team1)
                games.erase(i);
        }
    }
}

为什么当我请求数组游戏的成员(如.size()或.erase())时,我收到一条错误消息“请求成员 _ _在游戏中非类型int [6]“

1 个答案:

答案 0 :(得分:3)

普通的旧C数组没有成员。所以它没有sizeerase。如果 希望它拥有成员,C ++ 11提供了一个名为std::array<T,N>的类,只需执行std::array<int, 6> games = { ... }即可在您的案例中使用。但请注意,std::array没有erase成员函数,但它具有size成员函数。