c++ - 需要帮助调试从 const char* 到 char* [-fpermissive] 的无效转换

标签 c++

我是 c++ 的新手,不知道为什么会这样......

第 105 行我收到此错误从 const char* 到 char* 的无效转换 [-fpermissive] 第 113 行我收到从 âconst char* 到 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) { }        

最佳答案

这一行看起来是你的问题:

char* pszCr = strchr(pszText, 10);

您需要添加一个 constpszText开始是 const char * .您正在“ promise ”保持 pszText 的常量性你正在使用 strchr 的版本破坏接受并返回 char * .

关于c++ - 需要帮助调试从 const char* 到 char* [-fpermissive] 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8190988/

相关文章:

c++ - 分配二维可变大小的数组

c++ - 在 OSX 上使用不带 header 的 sinf()

c++ - 我在哪里初始化我的整数重要吗?

c++ - gcc vs. clang: "invalid use of incomplete type"with std::declval 和模板特化

c++ - SDL:延迟直到键盘输入

c++ - 我正在尝试创建的培训师出现问题(出于教育目的)

c++ - 字符串操作——将字符串追加到自身

c++ - C++ 中的命名空间

c++ - 从 C 字符串构造 std::string 与从另一个 std::string 构造不一致

c++ - 没有来自 wxWidget 应用程序的 cout,但是使用 Eclipse 它工作正常