Array of classes 'undefined' C++
Array of classes 'undefined' C++
那么,我有一个怪物数组和一个函数来显示它们的名字。问题是调试器说Mons(数组)是未定义的。我想知道我做错了什么。请帮助!错误大多是Mons是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 << "---------nn" << std::endl;
std::cout << "Monstersn--------" << 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[])
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- "Undefined class"作为is_base_of的论据
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- VSCode C/C++ Intellisense issue: Undefined identifiers (Linu
- 与卡特琳娜一起卷曲C++失败并得到"Undefined symbols for architecture x86_64"
- "Undefined symbols for architecture x86_64" 在 SDL 2 (macOS) 中
- 为什么在使用 SDL2 时仍然收到'undefined reference'链接器错误?
- 从 C 可执行文件加载动态库时收到错误C++"undefined symbol"
- 为什么即使在定义之后仍存在"Identifier is undefined error "?
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- 为什么当我在 C 程序中链接静态库时会"undefined symbol"?
- Vector, assign() function Undefined Behavior
- OpenCV imwrite 函数导致'undefined symbol'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- build error : undefined reference to `yyFlexLexer::yyFlexLex
- 错误说"camera is undefined",但我确实定义了它