本文介绍了为什么下面的C/C ++宏不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很简单,但我无法弄清楚.这是问题:我有一个返回字符串的简单函数:

It seems to be very simple, but I can't figure it out. Here is question:I have a simple function that returns a string:

const wchar_t* getCompanyName() { return L"Test Company";};

我想定义如下宏:

#define COMPANY getCompanyName();
#define PRODUCT COMPANY L" in Canada"

const wchar_t * company = COMPANY;
const wchar_t * product = PRODUCT;

我希望看到产品"值是加拿大的测试公司",但它仅显示"Test Company"和字符串"in Canada"永远不会与产品字符串相结合

I would expect to see the "product" value is "Test Company in Canada", but it only shows"Test Company" and string "in Canada" never concat to the product string

非常感谢您抽出宝贵的时间,以下是完整的代码:

Thank you so much for your time, here is the full code:

#include <stdio.h>
#include <tchar.h>

const wchar_t* getCompanyName() { return L"Test Company";};
#define COMPANY getCompanyName();
#define PRODUCT COMPANY L" in Canada"

int _tmain(int argc, _TCHAR* argv[]) {
    const wchar_t * company = COMPANY; // get Test Company
    const wchar_t * place = PRODUCT; // get Test Company in Canada

    wprintf(company);
    wprintf(place);

    return 0;
}

推荐答案

由于

#define COMPANY getCompanyName();

删除分号:

#define COMPANY getCompanyName()

详细说明一下您的书写方式:

To elaborate, the way you have it written:

const wchar_t * product = PRODUCT;

扩展为:

const wchar_t * product = getCompanyName(); L" in Canada";
L" in Canada";

本身就是一个很好的表达式,它什么也不做.故事的士气:宏中的分号要小心,通常它们不是必需的,但有时它们是必需的.

is a fine expression on its own that does nothing. Morale of the story: be careful with semicolons in macros, usually they are not necessary, but sometimes they may be.

这篇关于为什么下面的C/C ++宏不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:08