Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

no conversion function from 'std::__1::string' to 'std::__1::string *'

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading