2011-11-19 83 views
4

我是新來的C++,而且不知道爲什麼會這樣......需要幫助調試從爲const char *的無效轉換爲char * [-fpermissive]

線105我得到這個錯誤從常量無效的轉換字符*爲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) { }   
+3

如果您爲我們標記了第113行(特別是第113行只是一個大括號),T'會很好。 –

+4

如果你是C++的新手,並且你有指針,你做錯了。這絕對不是學習C++的愉快或教育方式。 –

回答

2

這行看起來是你的問題:

char* pszCr = strchr(pszText, 10); 

因爲pszTextconst char *,所以您需要在開頭添加const。你正在做出一個「承諾」,通過使用strchr的版本來保持pszText的穩定性,該版本需要並返回char *

+0

我剛剛發現,也想知道 - 'strchr(3)'的原型是:'char * strchr(const char * s,int c);' - 不應該從'strchr'賦值給'char *' (3)'工作好嗎? – sarnold

+0

糟糕。我把C與C++混淆--C('gcc')對賦值非常滿意,但C++('g ++')拋出一個錯誤信息:'從'const char *'無效轉換爲'char *'' – sarnold

+1

根據:http://www.cplusplus.com/reference/clibrary/cstring/strchr/,有兩個版本的strchr。 –

相關問題