當我在宏內部使用a->url
時,它失敗了,但是當我替換a->url
並且手動輸入字符串時,它可以工作。我如何使a->url
與宏兼容?C++ - 爲什麼在使用宏時此代碼無法工作?
g++ -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-MacOSX/main.o.d" -o build/Debug/GNU-MacOSX/main.o main.cpp
main.cpp:18:35: error: expected ';' after expression
cout << MANIFEST_URL(a->url);
CODE:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
#define MANIFEST_URL(REPLACE) "https://" REPLACE "/manifest.json";
typedef struct custom {
char *id;
string url;
custom *next;
} custom;
int main() {
custom *a;
a = new custom;
a->url = "www.google.com";
cout << MANIFEST_URL(a->url);
cout << a->url;
return 0;
}
想一下宏擴展,如果直接寫而不是宏,它真的有效嗎? –
還要記住,預處理是與實際編譯分開的一個步驟。預處理器對你的結構或變量一無所知。 –
而不是一個宏,定義一個使用字符串連接的函數。 –