【问题标题】:Compiler returns error after calling class from other file从其他文件调用类后编译器返回错误
【发布时间】:2021-10-28 07:53:23
【问题描述】:

我使用的是 Visual Studio 2019。我确实为路径设置了属性。该文件应该是从获胜/失败战斗菜单到用户新战斗的过渡。我想在我的主文件中调用它,但是当我编译主文件时,我得到与包含此类的 cpp 文件中相同的错误。我确实尝试过更改#pragma的位置并先移动我的cpp文件的#include,但它仍然不起作用(我这样做是因为我在微软的官方网站上找到了它)。

#pragma once
#include <iostream>
#include <stdlib.h>
#include "BattleMenu.cpp"
using namespace std;

class MenuTransition
{
public:
    int user_input;

    void transition()
    {
        cout << "You are encoutering a new enemy" <</*EnemyName*/endl;
        cout << "Press ENTER to continue..." << endl;
        cin >> user_input;
        BattleMenu menu;
        system("cls");
    }

    MenuTransition()
    {
        transition();
    }

    ~MenuTransition(){}
};
error C2065: 'BattleMenu': undeclared identifier
syntax error: missing ';' before identifier 'menu'
error C2065: 'menu': undeclared identifier

【问题讨论】:

  • 您可能想要#include "BattleMenu.h"?包含 cpp 文件几乎总是不正确的。
  • 与 cpp 相同的错误

标签: c++ class error-handling include


【解决方案1】:

你应该制作 BattleMenu 文件的 hpp 文件,并像 #include "BattleMenu.hpp" 一样包含它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多