使用命名空间时出错
Error when using namespace?
我正在研究C++和C#之间的dllexport和dllimport。
我读过这篇文章http://functionx.com/csharp2/libraries/cppcli.htm
我的C++库代码:
// Business.h
#pragma once
using namespace System;
namespace Business {
public ref class Finance
{
public:
double CalculateDiscount(double MarkedPrice,
double DiscountRate)
{
return MarkedPrice * DiscountRate / 100;
}
};
}
这里是C#代码:
using System;
using System.Runtime.InteropServices;
using Business;
namespace DepartmentStore
{
class Exercise
{
[DllImport("Business.dll")]
public static extern double CalculateDiscount(double price,
double discount)
static int Main()
{
Finance fin = new Finance();
double markedPrice = 275.50;
double discountRate = 25.00; // %
double discountAmount = fin.CalculateDiscount(markedPrice,
discountDate);
double netPrice = markedPrice - discountAmount);
Console.WriteLine("Marked Price: {0:C}", markedPrice);
Console.WriteLine("Discount Rate: {0:P}", discountRate / 100);
Console.WriteLine("Discount Amount: {0:C}", discountAmount);
Console.WriteLine("Net Price: {0:C}n", netPrice);
return 0;
}
}
}
但是它在构建时遇到了错误"The type or namespace name 'Business' could not be found (are you missing a using directive or an assembly reference?)"
。
有人可以告诉我怎么修。
非常感谢
DLL函数导入和导出使用C ABI。这个二进制接口对名称空间一无所知。此外,您创建的DLL使用C++/CLI,而不是C ABI。要使用它,您不会使用DLLImport
,而是在项目设置中引用DLL。
相关文章:
- 命名空间中具有.h和.cpp文件的类
- 从父命名空间重载类型
- 在命名空间中定义函数还是限定函数
- C++:对不存在的命名空间使用命名空间指令
- 通过继承类使用来自不同命名空间的运算符
- 使用命名空间时出现多个定义错误
- CUDA内核和数学函数的显式命名空间
- 嵌套的匿名命名空间
- CMakeLists.txt中的命名空间表示法
- 类是C++中的命名空间吗
- 在命名空间中使用全局命名空间中的函数
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- '使用命名空间{嵌套在另一个命名空间中的某个命名空间}"
- 使用匿名命名空间中的函数或另一个文件中的静态函数不应该出错吗?
- 在同一命名空间中声明变量和函数是否出错?[C++]
- 将指针添加到不同命名空间中的类时出错
- 内联命名空间函数时出错
- 使用命名空间时出错
- 在命名空间中调用静态函数时出错
- 使用嵌套命名空间声明"using namespace"时出错 ( "namespace xxx::yyy not allowed in using-declaration" )