Question

So I want to convert every instance of a \ into \\ for using in a function that creates directories.

string stripPath(string path)
{       
    string newpath;  
    for (int i = 0; i <= path.length() ;i++)
    {
        if(path.at(i) == '\\')
        {
            string someString( path.at(i) );
            newpath.append(path.at(i));
            newpath.append(path.at(i));
        }
    else
    newpath.append(path.at(i));
    }
    return newpath;
} 

newpath.append needs a string so I'm trying to create a string out of path.at(i). I get an error on Visual Studio that says no instance of constructor matches the argument list. I imported string already.

Here is the documentation for string:at. I'm quite confused because I think I'm doing it right?

http://www.cplusplus.com/reference/string/string/at/

Was it helpful?

Solution 3

The error concerns the call to append, it should be:

newpath.append(1, path.at(i));

OTHER TIPS

std::string doesn't have any constructor which uses a char& as an argument. The call should be:

string someString( 1,  path.at(i) );

Appending single characters is idiomatically done with the += operator:

newpath += path.at(i);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top