Visual Studio 2015中的宏参数限制

Macro parameter restrictions in Visual Studio 2015

本文关键字:参数 Studio 2015 Visual      更新时间:2023-10-16

我正在移植到windows与visual studio 2015的一些代码,构建在linux与gcc。

在VS中,我得到错误错误C2059:语法错误:'+='当使用带有参数"+="的宏DEFOP时,与其他参数相同。这是代码:

#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是一个非标准宏,因此并非所有编译器都接受它。使用宏参数作为字符串文字的常见方法是使用#,即您应该将__STRING(OP)替换为#OP

相关文章: