当我在宏内部使用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;
}
想一下宏扩展,如果直接写而不是宏,它真的有效吗? –
还要记住,预处理是与实际编译分开的一个步骤。预处理器对你的结构或变量一无所知。 –
而不是一个宏,定义一个使用字符串连接的函数。 –