Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: cannot convert const string to const char* for argument 1 to size_t strlen(const char*)

Tags:

c++

I'm pretty new to C++ and am very confused as to what is happening here. The error is the line int len = strlen(strin);. Any suggestions how to fix this would be much appreciated.

BigNum::BigNum(const std::string& strin)
{
    digits = NULL;

    int len = strlen(strin);

    if (len == 0)
    {
        BigNum zero;
        *this = zero;
        return;
    }

    used = len;
    positive = true;

    int i = 0;

    if(strin[i] == '-')  
    {
        positive = false;
        i = 1;
        used--;
    }
    else if(strin[i] == '+')  
    {
        i = 1;
        used--;
    }

    capacity = double_up_default(used);

    digits = new unsigned int[capacity];

    for(unsigned int k = 0; k < used; ++k) 
    {
        digits[used - k - 1] = strin[i++] - '0';
    }

    trim();
}
like image 235
user3403896 Avatar asked Dec 19 '22 17:12

user3403896


1 Answers

strlen knows nothing about std::string. It is a C function that returns the length of a null-terminated string.

Fortunately std::string knows its own length. Try this instead:

int len = strin.size();

or, if you care about the range of sizes a string may have,

std::string::size_type len = strin.size();
like image 52
juanchopanza Avatar answered Feb 16 '23 01:02

juanchopanza