Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need help debugging invalid conversion from const char* to char* [-fpermissive]

Tags:

c++

I'm new to c++ and have no idea why this is happening...

line 105 I get this error invalid conversion from const char* to char* [-fpermissive] line 113 I get error invalid conversion from âconst char* to char* [-fpermissive]

#include <cstdio>
#include <cstdarg>
#include <string>

using namespace std;

char acWordWrap[1024];
char acPrint[1024];

BasicConsole::BasicConsole(char* szName)
: ZFSubSystem(szName)
{
    m_iMaxWidth = 50;   //TEXT_MAX_LENGHT;
    m_bLog      = false;
}


BasicConsole::~BasicConsole()
{
    for(unsigned int i =0;i<m_kText.size();i++)
    {
        delete[] m_kText[i];    
    }

    m_kText.clear();
}


/** \brief  Prints a single row to console.
        \ingroup Basic
*/
void BasicConsole::PrintOneRow(const char* aText)
{
    ZFAssert(aText, "NULL Pointer argument");

    delete[] m_kText[m_kText.size()-1];

    for(int i=m_kText.size()-1;i>0;i--)
    {
        if(m_kText[i-1]!=NULL)
        {
            m_kText[i]=m_kText[i-1];            
        }
    }

    m_kText[0]=new char[m_iMaxWidth+2];

    strcpy(m_kText[0],aText);
}


/** \brief  Prints a row and word wraps so it don't go beoynd edge of console.
        \ingroup Basic
*/
void BasicConsole::PrintWordWrap(const char* aText)
{
    ZFAssert(aText, "NULL Pointer argument");

    string strEndLined = string(aText) + "\n";
    if(m_bLog)
        GetSystem().Log("console", strEndLined.c_str());

    int iNumOfChars = strlen( aText );

    while(iNumOfChars > m_iMaxWidth)
    {
        const char* szSpace = &aText[m_iMaxWidth];
        int iWidht  = m_iMaxWidth;

        while(szSpace > aText && szSpace[0] != ' ') szSpace--;
        if(szSpace == aText)
        {
            szSpace =  &aText[m_iMaxWidth];
        }
        else
        {
            iWidht  = szSpace - aText;
        }

        strncpy(acWordWrap, aText, iWidht);
        acWordWrap[iWidht] = 0;
        PrintOneRow(acWordWrap);
        aText += iWidht;
        iNumOfChars -= iWidht;
    }

    if(aText[0])
        PrintOneRow(aText);
}


/** \brief  Prints one row and handles splitting it up at line breaks.
        \ingroup Basic
*/
void BasicConsole::Print(const char* aText) 
{
    ZFAssert(aText, "NULL Pointer argument");

    const char* pszText = aText;

    char* pszCr = strchr(pszText, 10);
    while(pszCr)
    {
        strncpy(acPrint,pszText, pszCr - pszText);
        acPrint[pszCr - pszText] = 0;
      PrintWordWrap(acPrint);
        //cout << " - '" << acPrint << "'"<< endl;
        pszText = pszCr + 1;
        pszCr = strchr(pszText, 10);
    }

    if(pszText[0])
    {
        //cout << "- '" << pszText << "'"<< endl;
      PrintWordWrap(pszText);
    }
}


/** \brief  The function used to print to the console.
        \ingroup Basic
*/
void BasicConsole::Printf(const char *fmt, ...)
{
    ZFAssert(fmt, "NULL Pointer argument");

    // Make sure we got something to work with. 
    if (fmt == NULL)            
        return;                         

    va_list     ap;                         // Pointer To List Of Arguments

    va_start(ap, fmt);                      // Parses The String For Variables
        vsprintf(g_szFormatText, fmt, ap);      // And Convert Symbols
    va_end(ap);                             // 

    // Now call our print function.
    Print(g_szFormatText);
}

bool BasicConsole::StartUp()        { return true;  }
bool BasicConsole::ShutDown()       { return true;  }
bool BasicConsole::IsValid()        {   return true;    };      
void BasicConsole::RunCommand(int cmdid, const ConCommandLine* kCommand) { }        
like image 215
John Griffith Avatar asked Nov 05 '22 10:11

John Griffith


1 Answers

This line looks to be your problem:

char* pszCr = strchr(pszText, 10);

You need to add a const at the beginning of that since pszText is a const char *. You are making a "promise" to keep the const-ness of pszText that you are breaking by using the version of strchr that takes and returns char *.

like image 54
Jim Buck Avatar answered Nov 14 '22 22:11

Jim Buck