首页 文章

C宏上的警告太多

提问于
浏览
0

我正在尝试在我的代码上标记警告的方法,但我做了一些错误,无法理解在哪里 . 我的编译器给了我太多关于每次使用这个宏的警告:

#define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__);

#define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);

使用示例:

MPL_ERROR_MESSAGE("Test %d", 2);
In file included from ../trunk/mpl_position.h:5:0,
                     from ../trunk/mpl_baseenvironment.h:6,
                     from ../trunk/mpl_baseenvironment.cpp:1:
    ../trunk/mpl_baseenvironment.cpp: In member function ‘int MPL_BaseEnvironment::getEnemyRobotAt(MPL_Position&, int)’:
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_baseenvironment.cpp:28:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("(invalid value) not in [0,%d) ", i_ER, qlMPLP_enemyRobot__.size());
             ^
    ../trunk/mpl_baseenvironment.cpp: In member function ‘int MPL_BaseEnvironment::deleteEnemyRobotAt(int)’:
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_baseenvironment.cpp:45:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("(invalid value) not in [0,%d) ", i_ER, qlMPLP_enemyRobot__.size());
             ^
    ../trunk/mpl_baseenvironment.cpp: In member function ‘int MPL_BaseEnvironment::getMovableObstacleAt(MPL_Position&, int)’:
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__

编辑:更新警告列表

../trunk/mpl_gridenvironment.h: In member function ‘long int MPL_GridEnvironment::arrayIndex(MPL_Position&)’:
    ../trunk/mpl_utils.h:7:167: warning: unknown conversion type character ‘,’ in format [-Wformat=]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);
             ^
    ../trunk/mpl_utils.h:7:167: warning: unknown conversion type character ‘,’ in format [-Wformat=]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);
             ^
    ../trunk/mpl_utils.h:7:167: warning: unknown conversion type character ‘)’ in format [-Wformat=]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);
             ^
    ../trunk/mpl_utils.h:7:167: warning: too many arguments for format [-Wformat-extra-args]
     #define MPL_ERROR_MESSAGE2(output, fmt, ...) fprintf(output, ("\n[ERROR][%s:%d( %s )]\n---  " fmt "  ---\n\n"), __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); //error message
                                                                                                                                                                           ^
    ../trunk/mpl_utils.h:9:37: note: in expansion of macro ‘MPL_ERROR_MESSAGE2’
     #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__);
                                         ^
    ../trunk/mpl_gridenvironment.h:253:9: note: in expansion of macro ‘MPL_ERROR_MESSAGE’
             MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);

编辑2:按要求,其中一个触发警告的地方:

inline long arrayIndex(MPL_Position& sMPLP_p)
{
    long x, y, z;
    x = MPL_ROUND_INT((d_delta*sMPLP_p.getX())/d_sizeX__);
    y = MPL_ROUND_INT((d_delta*sMPLP_p.getY())/d_sizeY__);
    z = MPL_ROUND_INT((d_delta*sMPLP_p.getZ())/d_sizeZ__);

    if( (x>=0) && (x<n_sizeX__) && (y>=0) && (y<n_sizeY__) && (z>=0) && (z<n_sizeZ__) ){ //isValidPosition(...)
        return( (x*n_sizeY__ + y)*n_sizeZ__ + z );
    }

    MPL_ERROR_MESSAGE("Invalid Position at (%l, %l, %l)", x, y, z);

    MPL_GE_InvalidPosition(sMPLP_p);

    return( -1 );
}

(n_size?__和d_size?__是类属性;不要试图理解......:P)

2 回答

  • 0

    关于编辑中列出的警告:

    unknown conversion type character ‘,’ in format
    

    这个

    ... "Invalid Position at (%l, %l, %l)", ...
    

    应该

    ... "Invalid Position at (%ld, %ld, %ld)", ...
    

    你传递 long signed int . signed int 的转换说明符是 d . 当使用长度修饰符 l 传递 long 前缀时 .


    而且,虽然无关,但请从马科斯删除尾随 ; .

  • 0

    我发现了它们之间的区别

    #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, ##__VA_ARGS__)
    

    #define MPL_ERROR_MESSAGE(fmt, ...) MPL_ERROR_MESSAGE2(stderr, fmt, __VA_ARGS__)
    

    当没有参数使用时,比如

    MPL_ERROR_MESSAGE("Just a message! Hi!");
    

    第一个导致

    MPL_ERROR_MESSAGE2(stderr, "Just a message! Hi!", __VA_ARGS__)
    

    而第二个导致

    MPL_ERROR_MESSAGE2(stderr, "Just a message! Hi!", __VA_ARGS__, )
    

    哪个错了 . 该死的错了 . 并且不要编译,因为MPL_ERROR_MESSAGE2并不期望最后一个逗号 . 你知道为什么会这样吗?我应该发一个新问题吗?

相关问题