我需要將我的類宣告轉換為字串,我還需要定義類。在下面的代碼中,我給出了一個例子,其結果是Identifier Person is undefined或Incomplete type not allowed。但如果這是用自定義宏來實作的,一些代碼將非常感激。
struct Person;
std::string Person::meta = STRINGIFY(
struct Person{
static std::string meta;
std::string name = "Test";
int age = 5;
std::string address = "No:35179 Address";
};
);
人的人。
uj5u.com熱心網友回復:
你不能這樣做;你不能在定義型別之前初始化Person::meta,你也不能把型別定義為初始化運算式的一部分。
但是,你可以把初始化移到宏中:
#define WITH_META(cls, body) struct cls body; std::string cls::meta = "struct" #cls #body;
WITH_META(Person, {
static std::string meta;
std::string name = "Test"/span>;
int age = 5;
std::string address = "No:35179 Address";
});
int main()
{
std::cout << Person::meta << std::endl;
輸出:
struct Person{static std::string meta; std: :string name = "Test"; int age = 5; std::string address = "No:35179 Address";} {static; std::string meta =
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331860.html
標籤:
下一篇:C中的霍夫變換有問題
