我在 Visual Studio 2010 Professional 中创建了一个 C++/CLI (Visual C++) 项目。然后我在项目中添加了一个非常小的 C++ 类。以下是代码
#include <stdafx.h>
#include <iostream>
using namespace std;
class Tester
{
public:
Tester(){};
void show()
{
cout << "OKOK..Printing" << endl;
}
};
现在,我将一个按钮拖放到自动构建的 GUI 表单中,我将要从该按钮调用上述代码。以下是按钮的代码。
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Tester ^t = gcnew Tester();
//Test t; - giving errors as well
}
};
当我执行代码时,出现以下错误
1>------ Build started: Project: testdamn, Configuration: Debug Win32 ------
1>Build started 7/1/2013 12:59:38 PM.
1>InitializeBuildStatus:
1> Touching "Debug\testdamn.unsuccessfulbuild".
1>GenerateTargetFrameworkMonikerAttribute:
1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
1>ClCompile:
1> All outputs are up-to-date.
1> Test.cpp
1> testdamn.cpp
1>c:\users\yohan\documents\visual studio 2010\projects\testdamn\testdamn\Form1.h(79): error C2065: 'Tester' : undeclared identifier
1>c:\users\yohan\documents\visual studio 2010\projects\testdamn\testdamn\Form1.h(79): error C2065: 't' : undeclared identifier
1>c:\users\yohan\documents\visual studio 2010\projects\testdamn\testdamn\Form1.h(79): error C2061: syntax error : identifier 'Tester'
1> Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.86
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
我还注意到,当我从按钮中删除类调用时,程序构建良好。那么,如何从 C++/CLI 调用这些 C++ 类?