0

I am trying to make a c++ library for creating mini-console UIs and i get this error when i try to use my custom function :

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> > *')

here is my code (I work with multiple files) :

main.cpp (My test file)

#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 (The library that i'm working on)


/*
 * 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 (The header file for the miniUI.cpp file)

#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

My error when compiling

/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.

Please help me finding how to fix this "conversion" error (i'm a newbie)

Equalisys
  • 15
  • 4
  • `string text\[textLenght\];` is `string text[textLenght];` i suppose, an array of strings. It can decay to a pointer to the first element in the array. And you have a different variable of same name which is a single string only. Don't use globals and don't give different things the same name, this might be already sufficient to solve the issue – 463035818_is_not_an_ai Nov 03 '22 at 10:21
  • indeed `slachesDachesMiniUI` expects a pointer to a stirng and you pass it a string – 463035818_is_not_an_ai Nov 03 '22 at 10:21

1 Answers1

1

The function slachesDachesMiniUI is called with an object of the type std;:string

std::string text = "test";

slachesDachesMiniUI(4, 4, text);

However the function corresponding parameter has the array type string[]

string slachesDachesMiniUI(int lenght, int height, std::string text[]){ 
                                                   ^^^^^^^^^^^^^^^^^^

So the compiler issues an error.

Pay attention for example to the inconsistent

if (lenght > 20) {
    return "ERROR: Invalid lenght ! please choose a lenght beetween 1 and 15";
}

Why the length must be between 1 and 15 and not 1 and 20?

Also this for loop

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
};

invokes undefined behavior because you are trying to change an empty object of the type std::string. Moreover the loop changes the same elements of the array lines length times.

The following loop

for (int i = 1; i < height; i++) {                      // intermediate line constructor
                                                        // Calculate all the lines beetween the header and the footer
    lines[i] = '|';

Also invokes undefined behavior.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335