gpt4 book ai didi

c++ - 错误 C2679 : binary '[' : no operator found which takes a right-hand operand of type 'overloaded-function'

转载 作者:行者123 更新时间:2023-11-28 02:21:36 25 4
gpt4 key购买 nike

我正在尝试为学校实验室制作一个小游戏,但我遇到了困难。我知道代码不是最好的,但是关于如何解决这个问题的提示首先会很棒 :D我得到这个:error C2679: binary '[' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there are no acceptable conversion)
在第 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);
}
}

最佳答案

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

所以只需将“()”添加到GetXGetY:

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

关于c++ - 错误 C2679 : binary '[' : no operator found which takes a right-hand operand of type 'overloaded-function' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32256767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com