我正在尝试完成一个通过 C# 控制台应用程序使用 C++ 库的简单教程。 这里我有一个简单的类库:
cpp文件:
#include "stdafx.h"
#include "ClassLibraryCPP.h"
using namespace ClassLibraryCPP;
void myClass::test()
{
Console::WriteLine("hello cpp/cli!");
}
头文件:
#pragma once
using namespace System;
namespace ClassLibraryCPP
{
public class myClass
{
public:
void test();
};
}
现在这是我的 C# 控制台:
using ClassLibraryCPP;
namespace ConsoleApplicationCS
{
class Program
{
static void Main(string[] args)
{
myClass testClass;
testClass = new myClass();
testClass.test();
}
}
}
我明白了:
'myClass' does not contain a definition for 'test' and no extension method 'test' accepting a first argument of type 'myClass' could be found (are you missing a using directive or an assembly reference?)
但是我已经将 myClass
和 test
方法设为 public
那么为什么 .test()
对控制台应用程序?
更新: 添加 ref 来制作托管类,然后重建 C++ 项目(通过右键单击并重建而不仅仅是重建解决方案)解决了问题。 我还发现这个问题相关: C# code can't "see" the methods in my C++ dll 我仍然不明白这是如何工作的: https://www.youtube.com/watch?v=xTRTY-fOIe8&t=1800s
最佳答案
管理你的类(class):
public ref class myClass
{
public:
void test();
};
更改方法声明:
void ClassLibraryCPP::myClass::test()
{
Console::WriteLine("hello cpp/cli!");
}
关于c# - 为什么我收到 "' 某个类'不包含 'some method' 的定义“错误,而该类具有该方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42483656/