C++跨文件定义Enemy类指针数组的实现问题求助
Let's walk through fixing your dynamic enemy array issue step by step, plus cover a safer, modern C++ alternative to avoid similar headaches down the line.
First, Fix the Immediate Compiler Errors
Your code has two critical mistakes causing the E0349 and E0044 errors:
Incorrect dynamic array assignment
Ingame.cpp, you tried to assign the new array to a dereferenced pointer:*en = new CEnemy[currentFloor]; // E0349The variable
enis aCEnemy*(a pointer to the start of aCEnemyarray). When you usenew[], it returns a pointer to the first element of the array—you need to assign this directly toen, not dereference it first. Correct it to:en = new CEnemy[currentFloor];The original code tried to assign a pointer to a
CEnemyobject (via*en), which is invalid and caused the E0349 error.Wrong member access syntax
You useden[i]->setX(...), buten[i]is not a pointer—it's a directCEnemyobject (sinceenpoints to an array ofCEnemyinstances, not pointers to them). Use the dot operator (.) instead of the arrow operator (->):en[i].setX(stairX + rand() % 5); en[i].setY(stairY + rand() % 5); // Keep the boundary checks with dot syntax too: if (en[i].getX() < 0) en[i].setX(0); // ... rest of your checks ...This fixes the E0044 error, as you're now accessing members of the object directly.
Clean up memory to avoid leaks
Add adelete[]call in yourCGamedestructor to free the dynamic array when the game object is destroyed:CGame::~CGame() { delete[] en; // Always use delete[] for arrays allocated with new[] }
A Safer Alternative: Use std::vector
Manual dynamic arrays are error-prone (easy to forget delete[], or mix up delete vs delete[]). For modern C++, std::vector is a better choice—it handles memory management automatically and is more flexible.
Here's how to refactor your code:
Update
game.h:#include <vector> // Add this header at the top #include "Player.h" #include "Enemy.h" class CGame { public: // ... your existing public methods ... private: // ... existing private members ... std::vector<CEnemy> enemies; // Replace CEnemy* en; with this };Update
game.cpp'ssetupmethod:void CGame::setup() { // Resize the vector to hold exactly currentFloor enemy instances enemies.resize(currentFloor); mc.setX(width / 2); mc.setY(height / 2); stairX = rand() % width; stairY = rand() % height; for (int i = 0; i < currentFloor; i++) { enemies[i].setX(stairX + rand() % 5); enemies[i].setY(stairY + rand() % 5); // ... same boundary checks as before ... } }No manual cleanup needed!
Thestd::vectorwill automatically destroy all enemy objects and free memory when theCGameinstance is destroyed, so you can remove thedelete[] en;line from the destructor entirely.
Bonus: Fix a Hidden Bug in main.cpp
I spotted another issue that would cause runtime errors:
CGame game(); // This declares a function named game that returns a CGame, not an object!
Change this to properly declare a CGame instance:
CGame game;
Otherwise, your game.setup() call would fail because you're trying to call a method on a function declaration, not an object.
内容的提问来源于stack exchange,提问作者2xWollert

