c++ - 类数组 'undefined' C++

标签 c++ arrays class

所以,我有一个怪物数组和一个显示它们名字的函数。问题是调试器说 Mons(数组)未定义。我想知道我做错了什么。请帮忙! 错误主要是 Mons is undefined。

#include <iostream> 
#include <fstream>
#include <windows.h>
#include <time.h>

/**
RPG v0.1.6
-
**/

//-- WinAPI stuff
void ClearScreen()
  {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }



class Player {
    public:
    int HP;
    int MHP;
    int STR;
    int AGI;
    int RES;
    int XP;
    int LVL;
    Player() { HP = 8; MHP = 8; STR = 4; AGI = 3; RES = 2; XP = 0; LVL = 1; };
private:
};

struct DT {

void save_to_file(std::string filename, const Player& P)
{
    std::ofstream f( filename.c_str() );
    f << P.HP << std::endl;
    f << P.MHP << std::endl;
    f << P.STR << std::endl;
    f << P.AGI << std::endl;
    f << P.RES << std::endl;
    f << P.XP << std::endl;
    f << P.LVL << std::endl;
}

bool load_from_file(std::string filename, Player& P) {
  std::ifstream f( filename.c_str() );
  f >> P.HP;
  f >> P.MHP;
  f >> P.STR;
  f >> P.AGI;
  f >> P.RES;
  f >> P.XP;
  f >> P.LVL;
  if(f.good()){
    std::cout << "Success!" << std::endl;
  }
  else {
    std::cout << "Failure" << std::endl;
  }
  return f.good();
}

};

class Monster {
public:
    int HP;
    int MHP;
    int STR;
    int AGI;
    int RES;
    int XP;
    std::string name;
    ///For Calling
    Monster() { };
    ///Set variables for new battle
    Monster(int A, int B, int C, int D, int E, int F, std::string G){
    this->HP = A;
    this->MHP = B;
    this->STR = C;
    this->AGI = D;
    this->RES = E;
    this->XP = F;
    this->name = G;
    };
    void setStats(int A, int B, int C, int D, int E, int F, std::string G){
    this->HP = A;
    this->MHP = B;
    this->STR = C;
    this->AGI = D;
    this->RES = E;
    this->XP = F;
    this->name = G;
    };
};

int monstersalive = 3;




// -- Call Functions
void BtlDisplay(Player& P, Mons M[]);
void Battle(Player& P, Mons M[]);

int main()
{
    // -- Call Classes
    Player p;
    DT d;

    Monster Mons[3];
    for(int j = 0; j < 3; j++){
        Mons[j].setStats(9,9,9,9 + j,9,9,"Noob");
    }

    BtlDisplay(p,Mons);


    //Battle(p,m);


    d.save_to_file("GameData1.txt", p);
    d.save_to_file("GameData2.txt", p);
    d.save_to_file("GameData3.txt", p);



    return 0;
}

void BtlDisplay(Player& P, Mons M[]) //-- Displays battle stats
{
    ClearScreen();
    std::cout << "---------" << std::endl;
    std::cout << "L " << P.LVL << std::endl;
    std::cout << "HP " << P.HP << "/" << P.MHP << std::endl;
    std::cout << "---------\n\n" << std::endl;
    std::cout << "Monsters\n--------" << std::endl;
    for(int i = 0; i < 3; i++){
        if(M[i].HP > 0){

        }
    }
}

void Battle(Player& P, Mons M[])
{
    while(monstersalive > 0 && P.HP > 0){
        srand(time(NULL));
    }
}

最佳答案

void BtlDisplay(Player& P, Mons M[]);

这表示您要将数组传递给名为 M 的参数,其中元素的类型为 Mons

但是 Mons 不是类型,它是局部变量的名称。类型是 Monster

尝试

void BtlDisplay(Player& P, Monster M[])

甚至

void BtlDisplay(Player& P, Monster Mons[])

关于c++ - 类数组 'undefined' C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755723/

相关文章:

c++ - 调整 vector 大小并检索值,这是正确的还是在任何情况下都可能导致段错误?

javascript - 在 JavaScript 中使用排序方法时是否应该检查数组长度?

arrays - 为什么汇编代码在加总和之前将值从 %edx 复制到 %rcx?

c++ - "class prototypes"在 C++ 中可能吗?

C++ 错误 : expected type specifier before "class name"

c++ - 除了使用 vector 或 new 之外,还有其他方法可以创建动态大小的数组吗?

c++ - 为什么在leetcode上运行报错,在vs2019上却没有报错?

c++ - 元程序c++代码

Javascript插入数组而不引用

java - java中如何重写一个方法然后调用父类(super class)方法