You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++跨文件定义Enemy类指针数组的实现问题求助

Fixing Your Dynamic Enemy Array in the CGame Class

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:

  1. Incorrect dynamic array assignment
    In game.cpp, you tried to assign the new array to a dereferenced pointer:

    *en = new CEnemy[currentFloor]; // E0349
    

    The variable en is a CEnemy* (a pointer to the start of a CEnemy array). When you use new[], it returns a pointer to the first element of the array—you need to assign this directly to en, not dereference it first. Correct it to:

    en = new CEnemy[currentFloor];
    

    The original code tried to assign a pointer to a CEnemy object (via *en), which is invalid and caused the E0349 error.

  2. Wrong member access syntax
    You used en[i]->setX(...), but en[i] is not a pointer—it's a direct CEnemy object (since en points to an array of CEnemy instances, 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.

  3. Clean up memory to avoid leaks
    Add a delete[] call in your CGame destructor 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:

  1. 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
    };
    
  2. Update game.cpp's setup method:

    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 ...
        }
    }
    
  3. No manual cleanup needed!
    The std::vector will automatically destroy all enemy objects and free memory when the CGame instance is destroyed, so you can remove the delete[] 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:49:42