好的,我將在此前序言中說我在查看之前查看過很多類似的問題和答案。找不到DLL的C#命名空間
背景信息 使用Visual Studio專業2013
目標: 這個項目是我自己的娛樂,並嘗試學習儘可能多的,我可以。
我有一個名爲BinaryTree.h的本地C++頭文件,它只是一個簡單的數據結構,它使用遞歸邏輯來構建二叉搜索樹。它自己運作得很好。
我想在C#中使用它創建一個GUI。 (它並不真正有用或實用,我只是選擇它,也是因爲我想要的。此外,儘管二叉樹類中的邏輯非常複雜(ish),但我只需要調用2個方法,一個addNode方法和一個toString方法返回最大深度和節點數量)。
我選擇使用C++/cli包裝來實現這一點。一切似乎順利,構建成功並在我的項目的調試目錄中創建了一個.dll文件。
現在,我開始了C#方面的事情。我將.dll文件添加到引用。但是,當我輸入「using filename.dll」時我收到一個錯誤,說「找不到類型或名稱空間...」。
重申我做了一些研究。我發現(似乎在VS2010中)不同的目標框架可能會導致此錯誤。我查了我的,目標都是net4.5,所以這不是問題。
這裏是我的C++/cli包裝的代碼。也許它與使用模板有關?任何幫助表示讚賞。
#pragma once
#include "D:\Schoolwork 2015\Test Projects\CPPtoC#WrapperTest\CPPLogic\CPPLogic\BinaryTree.h"
using namespace System;
namespace BinaryTreeWrapper {
template<class Data>
public ref class BinaryTreeWrapperClass
{
public:
BinaryTreeWrapperClass(){ tree = new BinaryTree(); }
~BinaryTreeWrapperClass(){ this->!BinaryTreeWrapperClass(); }
!BinaryTreeWrapperClass(){ delete tree; }
//methods
void wrapperAddNode(Data)
{
tree->addNode(Data);
}
std::string wrapperToString()
{
return tree->toString();
}
private:
BinaryTree* tree;
};
}
截圖錯誤:
編輯 好了,所以這裏是一個奇怪的事情......我的原始文件剛剛建成的新代碼罰款和產生。 DLL文件。然而,我決定嘗試一個新的項目,因爲名字空間還沒找到。在上方移動代碼,並試圖建立,我碰到的4個錯誤:
錯誤1個錯誤C2955:「二叉樹」:使用類模板需要模板參數列表
錯誤2錯誤C2512:「二叉樹':沒有適當的默認的構造可用
錯誤3錯誤C2662:‘無效二叉樹:: ADDNODE(數據):‘從指針‘二叉樹’不能轉換’這個’到‘二叉樹&’
錯誤4錯誤C2662 :'std :: string BinaryTree :: toString(void)const':無法將'this'指針從'BinaryTre e'到'const BinaryTree &'
我完全複製了代碼,只是將名稱空間更改爲「TreeWrapper」,並將類名更改爲「TreeWrapperClass」。
爲了幫助,我從我的BinaryTree.h文件中加入了一段代碼。還有一些更多的定義了'NODE'類,但我並不想把它弄得比我需要的多。
經過進一步調查,似乎問題在於使用'通用'。如果我將它全部切換到'模板',它的構建就很好,但它不能用作C#中的引用(獲取命名空間錯誤)。我使用非常簡單的方法(無模板)構建了一個測試項目,並且能夠使用我在C#中創建的.dll包裝器。所以問題在於模板和泛型。
最後編輯 我,如果我更改代碼以啓動模板「詮釋」它工作得很好,我可以在C#中使用它找到。例如:
...
BinaryTreeWrapperClass(){ tree = new BinaryTree<int>(); }
....
private:
BinaryTree<int>* tree;
BinaryTree.h
template<class Data>
class BinaryTree
{
private:
Node<Data>* root;
unsigned int nNodes;
unsigned int maxDepth;
unsigned int currentDepth;
void traverse(Node<Data>*& node, Data data);
public:
BinaryTree();
~BinaryTree();
void addNode(Data);
std::string toString() const
{
std::stringstream sstrm;
sstrm << "\n\t"
<< "Max Depth: " << maxDepth << "\n"
<< "Number of Nodes: " << nNodes << "\n";
return sstrm.str(); // convert the stringstream to a string
}
};
template<class Data>
BinaryTree<Data>::BinaryTree() //constructor
{
//this->root = NULL;
this->root = new Node<Data>(); //we want root to point to a null node.
maxDepth = 0;
nNodes = 0;
}
template<class Data>
BinaryTree<Data>::~BinaryTree() //destructor
{
}
template<class Data>
void BinaryTree<Data>::addNode(Data data)
{
traverse(root, data); //call traverse to get to the node
//set currentDepth to 0
currentDepth = 0;
}
template<class Data>
void BinaryTree<Data>::traverse(Node<Data>*& node, Data data)
{
//increment current depth
currentDepth++;
if (node == NULL) //adds new node with data
{
node = new Node<Data>(data);
//increment nNode
nNodes++;
//increment maxDepth if current depth is greater
if (maxDepth < currentDepth)
{
maxDepth = currentDepth - 1; //currentDepth counts root as 1, even though its 0;
}
return;
}
else if (node->getData() >= data) //case for left, getData must be bigger. The rule is, if a number is equal to getData or greater, it is added to the left node
{
Node<Data>* temp = node->getLeftNode();
traverse(temp, data); //recursive call, going down left side of tree
node->setLeftNode(temp);
}
else if (node->getData() < data) //case for right, getData must be less
{
Node<Data>* temp = node->getRightNode();
traverse(temp, data);
node->setRightNode(temp);
}
return;
}
我創建了一個空的類庫項目並編譯它,然後導入到C#項目中,並且不能重複此問題。 Visual Studio立即看到新的名稱空間。我會建議通過構建選項運行,並確保'Common Language Runtime Support'設置爲'/ clr' – ahwm
這非常奇怪。我檢查以確保該選項設置爲/ clr並且是。 我也嘗試創建一個新的庫項目,以及相同的問題。 – Orannis
@ahwm這不是問題在這裏(我會寫一個答案來解釋這個代碼有什麼問題) –