C++迷你控制台UI库编译错误:std::string与std::string*类型转换失败
问题:std::string 转 std::string* 编译错误解决
我正在开发一个用于创建迷你控制台UI的C++库,调用自定义函数slachesDachesMiniUI时出现编译错误,提示无法将std::string转换为std::string*。以下是我的多文件代码及具体编译错误信息,请求帮忙解决该类型转换问题。
main.cpp(测试文件)
#include <iostream> #include <string> #include "miniUI.hpp" using namespace std; const int textLenght = 20; string text[textLenght]; int main(){ cout << "Running" << endl; std::string text = "test"; slachesDachesMiniUI(4, 4, text); return 0; };
miniUI.cpp(库文件)
/* * Filename: CGUI Lib/src/miniUI.cpp * Created Date: Wednesday, November 2nd 2022, 2:36:34 pm * Author: Theodore ROY * * Copyright (c) 2022 Théodore ROY * Licensed under the CC-BY-SA 4.0 International license */ #include "miniUI.hpp" #include <iostream> #include <string> using namespace std; // This Module will look like this : -------------------------------- // | Text here | // -------------------------------- string lines[20]; string slash = "|"; string slachesDachesMiniUI(int lenght, int height, std::string text[]){ //Slashes and dashes based Mini UI if (lenght < 0){ return "ERROR: Invalid lenght ! please choose a lenght beetween 1 and 15"; } if (lenght > 20) { return "ERROR: Invalid lenght ! please choose a lenght beetween 1 and 15"; } if (height < 3){ return "ERROR: Invalid height ! please choose a height beetween 3 and 15";; // Sizes verification } if (height > 20) { return "ERROR: Invalid height ! please choose a height beetween 1 and 15"; } for (int i = 0; i < lenght; i++){ lines[0] = (lines[0] + '-'); // Calculate line1 (only calculate the numbre of '-' characters to // print in the first line }; for (int i = 1; i < height; i++) { // intermediate line constructor // Calculate all the lines beetween the header and the footer lines[i] = '|'; for (int j = 0; j < lenght; j++){ if (i == (height / 2)){ // if the constrctor is at the half of the height string textLineSpaces; if (j == lenght / 2 - text->size() ) { cout << "test" << endl; } lines[i] = slash; // number of the line = | + lenght of the line / 2 times ' ' } }; } string returnedValue = lines[0] + lines[1] + lines[2] + lines[3]; return returnedValue; };
miniUI.hpp(头文件)
#ifndef MINIUI_INTEGRATION #define MINIUI_INTEGRATION #include <string> std::string slachesDachesMiniUI(int lenght, int height, std::string text[]); //function that create a little windows #endif //MINIUI_INTEGRATION
编译错误信息
/usr/bin/g++ -fdiagnostics-color=always -g "/Users/Theod/Desktop/CGUI-main/source code/main.cpp" -o "/Users/Theod/Desktop/CGUI-main/source code/main" /Users/Theod/Desktop/CGUI-main/source code/main.cpp:18:5: error: no matching function for call to 'slachesDachesMiniUI' slachesDachesMiniUI(4, 4, text); ^~~~~~~~~~~~~~~~~~~ /Users/Theod/Desktop/CGUI-main/source code/miniUI.hpp:6:17: note: candidate function not viable: no known conversion from 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') to 'std::string *' (aka 'basic_string<char, char_traits<char>, allocator<char> > *') for 3rd argument; take the address of the argument with & std::string slachesDachesMiniUI(int lenght, int height, std::string text[]); ^ 1 error generated.
解决方案
错误核心
函数slachesDachesMiniUI声明的第三个参数是std::string text[],在C++里数组参数会被自动转为std::string*(字符串指针),但你在main里传的是单个std::string对象,类型不匹配直接报错。另外main里同时定义了全局字符串数组和局部字符串变量,局部变量会覆盖全局的,逻辑混乱;函数内部用text->size()也说明你实际需要的是单个字符串,而非数组。
修复步骤
1. 修改函数的参数类型
把函数的第三个参数从字符串数组改成单个字符串的常量引用(用引用避免拷贝,效率更高):
miniUI.hpp 修改后:
#ifndef MINIUI_INTEGRATION #define MINIUI_INTEGRATION #include <string> std::string slachesDachesMiniUI(int lenght, int height, const std::string& text); #endif //MINIUI_INTEGRATION
miniUI.cpp 修改参数和内部代码:
// 替换参数为const std::string& text string slachesDachesMiniUI(int lenght, int height, const std::string& text){ // 原有长度、高度检查代码保留不变 for (int i = 1; i < height; i++) { lines[i] = '|'; for (int j = 0; j < lenght; j++){ if (i == (height / 2)){ string textLineSpaces; // 把text->size()改成text.size(),因为现在是引用不是指针 if (j == lenght / 2 - static_cast<int>(text.size()) ) { cout << "test" << endl; } lines[i] = slash; } }; } // 原有返回代码保留不变 };
2. 清理main函数的变量冲突
删掉全局的string text[textLenght],直接传递局部字符串:
main.cpp 修改后:
#include <iostream> #include <string> #include "miniUI.hpp" using namespace std; int main(){ cout << "Running" << endl; std::string text = "test"; slachesDachesMiniUI(4, 4, text); return 0; };
额外优化提示
- 函数里的全局数组
lines[20]建议改成局部变量,避免多次调用时残留上一次的状态。 - 长度检查的提示信息前后不一致(一会说1-15,一会说1-20),建议统一逻辑。
lenght拼写错误,正确拼写是length。
内容的提问来源于stack exchange,提问作者Equalisys
相关产品推荐
相关产品推荐

