将类引用回C ++/CLI Visual Studio 2010中的主窗体 [英] Reference a class back to the main form in C++/CLI Visual Studio 2010

查看:113
本文介绍了将类引用回C ++/CLI Visual Studio 2010中的主窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Visual Studio 2010 C ++/CLI编写的类.我正在尝试让班级以主要形式被人们所熟知.编译时出现错误:

I have a class I wrote in Visual Studio 2010 C++/CLI. I''m trying to get the class to be known by the main form. I get the error when I compile:

error C2065: ''Simulate'' : undeclared identifier


该类看起来像这样:

Simulate 是我在与主要形式分开的文件中创建的类:


The class looks something like this:

Simulate is the class I created in a file separate from the main form:

public ref class Simulate // In simulate.h
{
  // Initialize here
}


表格看起来像:


and the form looks something like:

public ref class Form1
{
  // The rest of the form stuff

  private: System::void button1_click()
   {
     Simulate ^simEvent = gcnew Simulate; <<<==== This is getting the error
     // More code.
   }
};

推荐答案

您必须先包含文件,然后才能使用其中定义的类型.在C ++/CLI中,对于当前项目中的任何类型,都必须像标准C ++中那样包含文件.

#include "simulate.h"
You must include the file before using a type defined inside it. In C++/CLI, you must include files as in standard C++ for any type inside the current project.

#include "simulate.h"


如果您需要Simulate类的C ++代码(也许您想使用现有的C ++代码),一种解决方案是为该代码提供一个DLL项目(无UI)在该DLL中),并使用C#编写的应用程序引用该C ++/CLI DLL,并在C#中执行UI内容.
If you need C++ code for Simulate class (maybe you want to uses existing C++ code), one solution would be to have a DLL project for that code (no UI in that DLL) and have the an application written in C# that reference that C++/CLI DLL and do the UI stuff in C#.


在第一个代码段中,;类声明
Well, in the first code snippet, a ; is missing after the class declaration
public ref class Simulate 
{ 
    /* code here */ 
};  // <-- missing ; in sample code above.


后缺少 在这种情况下,编译器将尝试将之后发现的任何内容追加到声明中...

接下来,您必须使用


In this is the case, then the compiler would try to append to the declaration whatever it find afterward...

Next, you will have to ensure that the file is indeed include using

#error testing inclusion...


确保确实包含该文件. 如果文件的相似名称在其他地方存在,则可能不会包含该文件,并且该文件已被拾取.同样,当使用include Guard来防止多次包含同一文件时,可能会发生这种情况.出现问题的一种常见情况是从另一个文件复制了一个文件,并且包含保护文件未正确更新.使用#pragma once可以消除此问题.包括警卫类似:

simulate.h


A file might not be include is a file similary named exists elsewhere and it picked up. Also, it might happen when include guard are uses to prevent multiple inclusion of the same file. A common case where this is a problem is when a file is copied from another one and include guard are not properly updated. This problem is eliminated is you uses #pragma once. Include guard are something like:

simulate.h

#ifndef SIMULATE_H     // Might be problematic if incorrect (after cut & paste)
#define SIMULATE_H

public ref class Simulate // In simulate.h
{
  // Initialize here
};

#endif



当代码不需要在不支持的编译器上可移植时,用#pragma once替换这3条多余的行会更容易.
要检查的另一件事是发生错误时正在编译哪个源文件.由于缺少的引用位于"Form1.h"中,因此可以假定在编译"Form1.cpp"时发生了错误,但如果文件是从多个位置包含的,则可能不是这种情况.而且,如果#include不在"Form1.h"内,并且其他使用"Form1.h"的源文件没有使用预编译的标头(不包含"stdafx.h"或设置为使用它们的选项),则可能会出错.

解决此问题的最佳方法是先清理"编译"stdafx.cpp",然后一个个地编译每个文件(从可疑文件(如"Form1.cpp"开始).这样做(以及#error技巧),您应该能够找到错误完成的操作.

重命名文件和/或类以查看问题是否仍然存在可能有助于识别可能的冲突.或者在某些情况下,四处移动代码或注释掉某些代码块.通常,这是我们根本看不到的小错误.所有这些技巧都可以在我们看不到原因时找到原因.



It is easier to replace those 3 extra lines with #pragma once when the code does not need to be portable on a compiler that does not support it.

Another thing to check is which source file is being compiled when the error occurs. Since the missing reference is in "Form1.h" one can assume that the error was occuring while compiling "Form1.cpp" but if the file is included from multiple location this might not be the case. And if the #include is not inside "Form1.h" and the other source file that uses "Form1.h" does not uses precompiled headers (does not include "stdafx.h" or options set to use them), then it can go wrong.

The best way to find such problem is to do a "clean" the compile "stdafx.cpp" and then compile each file one by one (starting by the suspect ones like "Form1.cpp"). Doing that (and also with the #error trick), you should be able to find what is done incorrectly.

Renaming files and/or class to see if the problem persist might help to identify possible conflict. Or in some case move code around or comment out some blocks of code. Often it is a small mistake that we simply does not see. All those tricks help to find the cause when we don''t see it.


这篇关于将类引用回C ++/CLI Visual Studio 2010中的主窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆