search

Home  >  Q&A  >  body text

c++ define 报错

#include <iostream>
#include <string>
using namespace std;
int main()
{

#define NEW_APP_PATH "C:\\QQ\\QQ.exe";
#define VAL_OPEN NEW_APP_PATH.substr(0,NEW_APP_PATH.find_last_of('\\'));
cout<<VAL_OPEN;
 getchar();
return 0;
}

用define 为什么会报错?

伊谢尔伦伊谢尔伦2805 days ago641

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 11:09:36

    NEWAPPPATH.substr NEWAPPPATH is a char[] array and does not support . operation; it can be modified to NEWAPPPATH string("C :QQQQ.exe") give it a try.

    reply
    0
  • PHPz

    PHPz2017-04-17 11:09:36

    Because NEWAPPPATH is a C string, and of course C string does not have the substr method.

    reply
    0
  • 黄舟

    黄舟2017-04-17 11:09:36

    The whole program is wrong, not just the define problem
    C ’s native strings have no member functions
    There is no semicolon at the end of the macro definition
    #define NEWAPPPATH std::string("C:QQQQ.exe")
    #define VALOPEN NEWAPPPATH.substr(0,NEWAPPPATH.findlast_of(''))
    This allows the compiler to reluctantly execute

    reply
    0
  • Cancelreply