请求成员“c_str”中的“str”,这是非类的

request for member ‘c_str’ in ‘str’, which is of non-class

本文关键字:str 是非 中的 成员 请求      更新时间:2023-10-16
main.cpp:561:80: error: request for member ‘c_str’ in ‘str’, which is of non-class type ‘std::string [10] {aka std::basic_string<char> [10]}’
   displayFont.showHighscore[i] = TTF_RenderText_Solid(displayFont.menuFont,str.c_str(), displayFont.colorText);

大家好!

根据此函数,这将显示包含从成员类派生的数据的排名。这也使用了来自另一个类(displayFont(的ttf内容。当我编译这个时,说了一个错误。我留下代码:

void showHighscore() {
    displayFont.menuFont = TTF_OpenFont("fonts/Lanehum.ttf",25);
    displayFont.colorText = { 255, 255, 255 }; // Add content
    stringstream texting[10]; // Variables
    string str[10];
    SDL_Rect posHighscore[10];
    for (int i = 0; i < 10; i++) {
        texting[i] << i << "#    - " << dat.topScore[i];
        str[i] = texting[i].str();
        displayFont.showHighscore[i] = TTF_RenderText_Solid(displayFont.menuFont,str.c_str(), displayFont.colorText); // OUTPUTTING ERROR :/
        posHighscore[i].x = 50; 
        posHighscore[i].y = 50 * (i+1);
    }
    while (true) {
        for (int i = 0; i < 10 ; i++) {
            SDL_BlitSurface(displayFont.showHighscore[i],NULL,screen,&posHighscore[i]);     
        } // Show the rendered text
        SDL_Flip(screen);
        SDL_Delay(5000);
        break;
    }
}

str是一个string数组:

 string str[10];

所以你不能打电话给str.c_str().你需要在str的一个元素上调用它,即使用索引:

str[i].c_str()

你根本不需要数组:

stringstream texting;
texting << i << "#    - " << dat.topScore[i];
displayFont.showHighscore[i] = TTF_RenderText_Solid(displayFont.menuFont,
                                                    texting.str().c_str(),
                                                    displayFont.colorText);

或者你也可以尝试string opt(str); 它实际上会将str的值存储在opt中,你可以通过opt.c_str()调用它