错误C2679:二进制'[':找不到运算符,它采用'overloaded-function'类型的右手操作数

时间:2015-08-27 18:20:01

标签: c++

我正在尝试为学校实验室制作一个小游戏而且我卡住了。我知道代码不是最好的,但是关于如何解决这个问题的提示首先会很棒:D 我得到了这个:错误C2679:二进制'[':找不到运算符,它采用'overloaded-function'类型的右手操作数(或者没有可接受的转换)
在第183和190行。任何想法?

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <ctime>
#include <vector>
#include <string>
using namespace std;
class monstru{
public :
    int x;
    int y;
    monstru(int X, int Y);
    int GetX();
    void SetX(int);
    int GetY();
    void SetY(int);
};
monstru::monstru(int X, int Y){
    x = X;
    y = Y;
}
int monstru::GetX(){
    return x;
}
int monstru::GetY(){
    return y;
}
void monstru::SetX(int X){
    x = X;
}
void monstru::SetY(int Y){
    y = Y;
}
class capcana{
public:
    int x;
    int y;
    capcana(int X, int Y);
    int GetX();
    void SetX(int);
    int GetY();
    void SetY(int);
};
capcana::capcana(int X, int Y){
    x = X;
    y = Y;
}
int capcana::GetX(){
    return x;
}
int capcana::GetY(){
    return y;
}
void capcana::SetX(int X){
    x = X;
}
void capcana::SetY(int Y){
    y = Y;
}
int  Verificare(vector<monstru>& Nmonstru, vector<capcana>& Ncapcana, vector< vector<char> > map);
void AdaugareMonstru(vector<monstru>& monstru);
void AdaugareCapcana(vector<capcana>& Ncapcana);
int main()
{  
    char player = 'P';
    int posX = 1, posY = 1,temp=0;
    char treasure = 'X';
    //vector<char> map[7][10];
    vector< vector<char> > map(7, vector<char>(10));
    vector<monstru> M;
    vector<capcana> C;
    //vector<monstru> B;
    AdaugareMonstru(M);
    AdaugareCapcana(C);
    //Initializing the map
    for (int i = 0; i<7; i++){
        for (int j = 0; j<10; j++){

            map[i][j] = '.';
        }
    }
    //Drawing the Map
    for (int i = 0; i<7; i++)
    {
        for (int j = 0; j<10; j++)

            cout << map[i][j] << " ";
        cout << endl;
    }

    while (true)
    {
        char move;
        cin >> move;

        switch (move)
        {
            // move up;
        case 'w':
            if (posY <= 0)
            {
                cout << endl;
                cout << "Character is going out of the range! Try again";
                cout << endl;
                cout << endl;
                break;
            }
            else
            {
                map[posY][posX] = '.';
                posY -= 1;
                map[posY][posX] = player;
            }
            break;
            // move down;
        case 's':
            if (posY >= 6)
            {
                cout << endl;
                cout << "Character is going out of the range! Try again";
                cout << endl;
                cout << endl;
                break;
            }
            else
            {
                map[posY][posX] = '.';
                posY += 1;
                map[posY][posX] = player;
            }
            break;
            // move left;
        case 'a':
            if (posX <= 0)
            {
                cout << "Character is going out of the range! Try again";
                cout << endl;
                cout << endl;
            }
            else
            {
                map[posY][posX] = '.';
                posX -= 1;
                map[posY][posX] = player;
            }
            break;
            //move right
        case 'd':
            if (posX >= 9)
            {
                cout << "Character is going out of the range! Try again";
                cout << endl;
                cout << endl;
            }
            else
            {
                map[posY][posX] = '.';
                posX += 1;
                map[posY][posX] = player;
            }
            break;
        }

        //Re-drawing the map for each turn
        for (int i = 0; i < 7; i++)
        {
            for (int j = 0; j < 10; j++)

                cout << map[i][j] << " ";
            cout << endl;
        }


        temp = Verificare(M, C, map);
        if (temp != 0)
            break;
    }

}
int  Verificare(vector<monstru>& Nmonstru, vector<capcana>& Ncapcana, vector< vector<char> > map){
    int temp;
    for (int i = 0; i < 4; i++){
        if (map[Nmonstru[i].GetX][Nmonstru[i].GetY] == 'P'){ 
                       ** I get error here ^^^^ **
                       ** I get error here ^^^^ **
                       ** I get error here ^^^^ **
            cout << "Ai fost mancat de monstru! Ai pierdut!";
            temp = 1;
        }
    }
    for (int i = 0; i < 4; i++){

        if (map[Ncapcana[i].GetX][Ncapcana[i].GetY] == 'P'){
                       ** I get error here ^^^^ **
                       ** I get error here ^^^^ **
                       ** I get error here ^^^^ **
            cout << "Ai cazut intr-o capcana! Ai pierdut!";
            temp = 2;
        }
    }
    if (map[6][9] == 'P')
    {
        cout << "Ai gasit comoara! Ai castigat";
        temp = 3;
    }

}
void AdaugareMonstru(vector<monstru>& Nmonstru){

        srand(time(0));
        for (int i = 0; i < 4; i++){
            monstru newMonstru(rand() % 6, rand() % 9);
            Nmonstru.push_back(newMonstru);
        }
    }
void AdaugareCapcana(vector<capcana>& Ncapcana){

    srand(time(0));
    for (int i = 0; i < 4; i++){
        capcana newCapcana(rand() % 6, rand() % 9);
        Ncapcana.push_back(newCapcana);
    }
}

1 个答案:

答案 0 :(得分:2)

你班级的

GetXGetY是函数。不是成员变量。要调用y函数,您需要在函数名后面添加括号(..)

只需将“()”添加到GetX广告GetY

    if (map[Ncapcana[i].GetX()][Ncapcana[i].GetY()] == 'P'){