请求成员“c_str”中的“str”,这是非类的
request for member ‘c_str’ in ‘str’, which is of non-class
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()
调用它
相关文章:
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- Python str to C++ to Python str
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 字符串变量,比如说"字符串str",可以直接复制到数组中吗?
- 解释一下 for (char c : str) 的作用?
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 返回 str vs. str.substr(0,str.size()) 在 leetcode 中给了我不同的输出
- remove(str.begin(), str.end(), );无法正常工作(我正在使用视觉工作室 2012)
- Getline(cin,str)的奇怪输出
- NVCC 错误:string_view.h:constexpr 函数返回是非常量
- 编译器是否必须始终删除 try-catch 块(如果它被证明是非抛出的)
- C++ 电话号码字母拨号程序 - 语法错别字?还是非 ASCII 字符?
- 比使用 s.str().c_str() 更好的表达?
- 除以 str.length() 什么也没显示
- 什么是非营利组织???我的问题是我不明白为什么有人会使用它
- 没有'str'成员在 GCC 和 Clang 'std::basic_ostream<char>',但 MSVC 没有问题
- 为什么带有指针子对象的文字类类型的 constexpr 表达式不能是非类型模板参数
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- 请求成员“c_str”中的“str”,这是非类的