【发布时间】:2015-10-02 11:28:37
【问题描述】:
我正在使用 Visual Studio 2015 将一些使用 gcc 在 linux 中构建的代码移植到 Windows。
在 VS 中,当使用带有参数“+=”的宏 DEFOP 时,出现错误 error C2059: syntax error: '+=',与其他参数相同。这是代码:
#define DEFOP(OP) \
Matrix& Matrix::operator OP (const double& val) { \
for (int i = 0; i < _n; i++) { \
_data[i] OP val; \
} \
return *this; \
} \
Matrix& Matrix::operator OP (const Matrix& that) { \
if (_rows != _rows || _cols != that._cols) { \
throw Exception (String ( \
"Matrix size mismatch in operation '%s': " \
"(%d,%d) vs. (%d,%d).", \
__STRING(OP), _rows, _cols, that._rows, that._cols)); \
} \
for (int i = 0; i < _n; i++) { \
_data[i] OP that._data[i]; \
} \
return *this; \
}
DEFOP(+=);
DEFOP(-=);
DEFOP(*=);
DEFOP(/=);
#undef DEFOP
有人知道如何修改这段代码以便在 VS 中构建吗?
【问题讨论】:
-
如果将
__STRING(OP)替换为#OP会怎样? -
我不会以这种方式使用宏。一开始很难调试。为什么不直接写出代码。操作简单,所有编辑器都有搜索和替换功能。
-
PS:
_rows != _rows是一个开始的错误 -
#OP 工作!我想知道这种相同的语法是否适用于 gcc。该代码也旨在构建在 gcc 中。 @SingerOfTheFall 如果您将此作为答案,我会接受。
-
所以这个“第三方”给你的代码不能编译?似乎比较奇怪。为什么不让他们修复它?
标签: c++ visual-studio gcc macros