【问题标题】:error C2679: binary '[' : no operator found which takes a right-hand operand of type 'overloaded-function'错误 C2679:二进制“[”:未找到采用“重载函数”类型的右侧操作数的运算符
【发布时间】:2015-08-27 18:20:01
【问题描述】:

我正在尝试为学校实验室制作一个小游戏,但遇到了困难。我知道代码不是最好的,但是关于如何解决这个问题的提示首先会很棒:D 我得到这个:error C2679: binary '[' : no operator found 它采用'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);
    }
}

【问题讨论】:

    标签: c++


    【解决方案1】:

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

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

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

    【讨论】:

    • 没问题!这就是我们在这里的原因。 :)
    • @TeoAlex ,在 StackOverflow 上,如果您喜欢一个答案,投赞成票被认为是礼貌的(单击左边距数字上方的三角形)。在提出您认为已完全回答的问题后,单击数字下方的复选标记(勾选符号)选择该答案也是一种礼貌。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多