C++ Visual Studio 中的错误 C2061

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14541193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:25:39  来源:igfitidea点击:

Error C2061 In visual studio

c++

提问by James Hurley

I try to reference a struct from another class in my code and it gives me an error, saying I have a syntax problem.

我尝试在我的代码中引用另一个类的结构,但它给了我一个错误,说我有语法问题。

#pragma once

#include "Definitions.h"

#include "GV.h"
#include "UI.h"
#include "Tile.h"
#include "Item.h"

class Figure {
public:
    //Figure index
    struct FIGURE_TYPE {
        //Where to crop the image from
        SDL_Rect crop;
        int x;
        int y;
    };

    //The game figure
    FIGURE_TYPE figure_index[FIGURE_COUNT];

    //The figure array
    int figure_array[MAP_HEIGHT / 64][MAP_WIDTH / 64];

    //Functions
    Figure ( void );
    bool draw_figures ( SDL_Surface* screen, SDL_Surface* figures, SDL_Rect camera, Figure::FIGURE_TYPE figure_spec[FIGURE_COUNT] );
};

That's the struct in Figure.h,

这就是Figure.h中的结构体,

#pragma once

#include "Definitions.h"

#include "GV.h"
#include "Tile.h"
#include "Item.h"
#include "Figure.h"

class UI {
public:
    UI ( void );
    void set_camera ( SDL_Rect& camera, Figure::FIGURE_TYPE figure_spec[FIGURE_COUNT] );
    bool draw_map ( SDL_Surface* screen, SDL_Rect& camera, SDL_Surface* tiles, SDL_Surface* figures, Figure::FIGURE_TYPE figure_spec[FIGURE_COUNT] );
    bool draw_status ( void );
};

And that is where I reference it, from another header file called UI.h. I know there is a problem with referencing structures, I just don't know how to fix it. Simple problem, any one wanna help?

这就是我从另一个名为 UI.h 的头文件中引用它的地方。我知道引用结构有问题,我只是不知道如何解决它。简单的问题,有人想帮忙吗?

The problem is notthat Figure Type is declared outside of Figure.h, or that it is private as opposed to public.

问题在于图类型是在 Figure.h 之外声明的,或者它是私有的而不是公共的。

Error Reports

错误报告

Error 1 error C2653: 'Figure' : is not a class or namespace name c:\users\jim\documents\c++\roguelike\roguelike\ui.h 13 1 roguelike

Error 3 error C2653: 'Figure' : is not a class or namespace name c:\users\jim\documents\c++\roguelike\roguelike\ui.h 14 1 roguelike

Error 2 error C2061: syntax error : identifier 'FIGURE_TYPE' c:\users\jim\documents\c++\roguelike\roguelike\ui.h 13 1 roguelike

Error 4 error C2061: syntax error : identifier 'FIGURE_TYPE' c:\users\jim\documents\c++\roguelike\roguelike\ui.h 14 1 roguelike

错误 1 ​​error C2653: 'Figure' : is not a class or namespace name c:\users\jim\documents\c++\roguelike\roguelike\ui.h 13 1 roguelike

错误 3 error C2653: 'Figure' : is not a class or namespace name c:\users\jim\documents\c++\roguelike\roguelike\ui.h 14 1 roguelike

错误 2 错误 C2061:语法错误:标识符 'FIGURE_TYPE' c:\users\jim\documents\c++\roguelike\roguelike\ui.h 13 1 roguelike

错误 4 错误 C2061:语法错误:标识符 'FIGURE_TYPE' c:\users\jim\documents\c++\roguelike\roguelike\ui.h 14 1 roguelike

回答by Adam Rosenfield

You have a circular dependency: UI.h depends on Figure.h, and Figure.h depends on UI.h. You need to break the circular dependency by removing the #includeof one file in the other. In this case, since I don't see anything in Figure.h using anything in UI.h, you should just remove the #include "UI.h"from Figure.h and be all set.

你有一个循环依赖:UI.h 依赖于 Figure.h,而 Figure.h 依赖于 UI.h。您需要通过删除#include另一个文件中的来打破循环依赖。在这种情况下,由于我没有看到 Figure.h 中的任何内容使用 UI.h 中的任何内容,因此您应该#include "UI.h"从 Figure.h 中删除并进行所有设置。

回答by Lightness Races in Orbit

Your syntax is fine.

你的语法很好。

What is not fine is that you have a circular dependency between your headers, and this is breaking your #includes.

不好的是,您的标题之间存在循环依赖关系,这破坏了您的#includes。

In this case, it's leading to Figurenot being visible from within "UI.h"; even though your syntax is correct, this causes the errors you've seen because "UI.h" doesn't knowthat your syntax is correct, because it doesn't know what Figureis.

在这种情况下,它导致Figure在“UI.h”中不可见;即使你的语法是正确的,这也会导致你看到的错误,因为“UI.h”不知道你的语法是正确的,因为它不知道什么Figure是正确的。

Do not have circular dependencies. Use forward declarationswhere possible to help you.

没有循环依赖。尽可能使用前向声明来帮助您。