简体   繁体   English

C ++ Visual Studio 2008中未声明的标识符

[英]undeclared identifier in C++ Visual Studio 2008

I have a C++ project in Visual Studio 2008. 我在Visual Studio 2008中有一个C ++项目。

In the project I have several forms and several non-form classes. 在项目中,我有几种形式和几种非形式类。 One non-form specifically called Import_LP.h that is a class with several methods all of which are written in the header file with nothing in the resource file. 一个非形式的专门称为Import_LP.h,它是一个具有多个方法的类,所有这些方法都写在头文件中,资源文件中没有任何内容。

I have no problem with #include Import_LP in any of the form classes and creating objects and referencing any of its methods, however any other class I try to #include it into, it gives me a 我在任何表单类中使用#include Import_LP都没有问题,并且创建对象并引用它的任何方法,但是我尝试#include它的任何其他类,它给了我一个

syntax error : undeclared identifier 'Import_LP'

on the line it is referenced occurs ie Import_LP^ importLP; 在它被引用的行上发生,即Import_LP ^ importLP;

I come from a java/c# background is there something I'm missing with the linking here? 我来自java / c#背景,这里有链接吗?

I was able to resolve the problem by random chance. 我能够通过随机机会解决问题。

So apparently you can't have two files include each other? 那么显然你不能有两个文件包含对方?

I was doing 我在做

#include Window.h

in Import_LP.h 在Import_LP.h中

and

#include Import_LP.h

in Window.h. 在Window.h中。

I would appreciate it if someone could explain to me why you can't do this. 如果有人能向我解释为什么你不能这样做,我将不胜感激。

If you have include guards, it goes like this: the preprocessor includes Import_LP.h, which says "only include me once", then includes Window.h, which tries to include Import_LP.h, but doesn't because of the include guard. 如果你有包含警卫,它是这样的:预处理器包括Import_LP.h,它说“只包括我一次”,然后包括Window.h,它试图包含Import_LP.h,但不包括因为包含保护。 So Window.h starts parsing the window class, but fails because the Import_LP.h class header hasn't fully loaded yet. 所以Window.h开始解析窗口类,但是因为Import_LP.h类头尚未完全加载而失败。

The solution is to predeclare the classes: 解决方案是预先声明类:

Window.h: 在window.h:

#ifndef WINDOW_H  //works best if this is first
#define WINDOW_H
#pramga once

class Import_LP;
class Window {
    Import_LP* member; //member has to be a pointer
    void func();
};
#include "Import_LP.h"
inline void Window::func() {  
}
#endif WINDOW_H

Import_LP.h: Import_LP.h:

#ifndef IMPORT_LP_H //works best if this is first
#define IMPORT_LP_H 
#pramga once

class Window;
class Import_LP {
    void func(Window& parent); //parameter has to be a pointer or reference
};
#include "Window.h"
inline void Import_LP::func(Window* parent) {  
}
#endif IMPORT_LP_H 

This will only allow you to reference the other by pointer or reference until the actual include, but that should be doable. 这只允许您通过指针或引用引用另一个,直到实际包含,但这应该是可行的。 Technically you only have to do this to one or the other headers. 从技术上讲,您只需要对一个或其他标题执行此操作。

Sounds like the type Import_LP is undefined, your challenge is to figure out why. 听起来像Import_LP类型未定义,你的挑战是找出原因。 First thing to do is to read the contents of import_LP.h and figure out how Import_LP is declared. 首先要做的是阅读import_LP.h的内容并弄清楚如何声明Import_LP。 One possible approach would be to open one of the good files, right click on a use of Import_LP and select "Go To Declaration". 一种可能的方法是打开一个好的文件,右键单击Import_LP的使用并选择“Go To Declaration”。 That should move the focus to the declaration of Import_LP that applies in that specific case. 这应该将重点转移到适用于该特定情况的Import_LP声明。

It could be the declaration is surrounded by a #ifdef that somehow prevents it being compiled in your other files. 它可能是声明被#ifdef包围,以某种方式阻止它在您的其他文件中编译。 Or it could be something else, we don't really have enough details to work with. 或者它可能是其他东西,我们没有足够的细节可以使用。

Update: 更新:

So apparently you can't have two files include each other? 那么显然你不能有两个文件包含对方? . . . I would appreciate it if someone could explain to me why you can't do this. 如果有人能向我解释为什么你不能这样做,我将不胜感激。

You can end up defining types and variables more than once when you do that. 当你这样做时,你最终可以多次定义类型和变量。

There are techniques like #include guard and #pragma once which may be used to mitigate these sorts of problems. 有一些技术,比如#include guard#pragma ,可以用来缓解这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM