我正在開發一個依賴于 C /CLI 專案的 C# 專案。我正在使用一些文字成員(它們是 C# 中的常量),使用諸如int或unsigned short我的 C# 專案可以作為常量訪問的型別。我想對字串做同樣的事情。由于在 C# 中是可能的,我嘗試在 C 中做到這一點......然后我遇到了問題。
using namespace System;
namespace TestNamespace
{
static ref class TestClass
{
literal char* TestString = "fish";
};
}
IntelliSense 不會給我錯誤,但是當構建此代碼時,會出現此錯誤:“'TestNamespace::TestClass::TestString':不能是文字資料成員”
我已將 char* 更改為 const char*、std::string 和指向其他型別字符的指標。我已經在互聯網上對這些東西做了很多挖掘并了解其中的一些,但我不知道為什么這不起作用。我還閱讀了有關字串文字的內容,并認為這會對我有所幫助。好東西知道,但不一定在這種情況下。
uj5u.com熱心網友回復:
.NET 中的字串是System::String^資料型別,因此您需要將其宣告為該型別,而不是char*.
using namespace System;
namespace TestNamespace
{
static ref class TestClass
{
literal String^ TestString = "fish";
};
}
現在TestString將在 C# 中顯示為TestNamespace.TestClass.TestString.
uj5u.com熱心網友回復:
在 C 中執行此操作的正確方法是:
namespace TestNamespace
{
class TestClass
{
const char* TestString = "fish";
};
}
因此,我們有所謂的命名空間TestNamespace內,我們有一個叫做類TestClass具有型別的資料成員const char*命名TestString。
你也可以這樣做:
namespace TestNamespace
{
class TestClass
{
std::string TestString = "fish";
};
}
現在變數的型別TestString是std::string.
另請查看C# 和 C 之間的共享變數
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372361.html
