Author Topic: Compile errors  (Read 9573 times)

sethjackson

  • Guest
Compile errors
« on: November 07, 2005, 01:39:18 am »
I'm trying to convert text to HTML.

Here is the code.

m_pTextCtrl is a wxTextCtrl.

Code
wxString MainFrame::TextToHTML()
{
    const wxString sOO = _T("00");
    const wxString s8O = _T("80");
    const wxString sFF = _T("FF");

    wxString htmlString;

    htmlString << _T("<html><head><title></title></head><body><tt>");

    long max = m_pTextCtrl->GetLastPosition();

    wxTextAttr style;

    wxTextAttr oldStyle;

    wxChar ch;

    for(long pos = 0; pos < max; pos++)
    {
        m_pTextCtrl->GetStyle(pos, style);

        if(style != oldStyle)
        {
            if(pos > 0)
            {
                htmlString << _T("</font>");
            }

            wxString sR, sG, sB;

            wxColour colour = style.GetTextColour();

            sR.Printf(_T("%02X"), colour.Red());
            sG.Printf(_T("%02X"), colour.Green());
            sB.Printf(_T("%02X"), colour.Blue());

            htmlString << _T("<font color = \"#") << sR << sG << sB << _T("\">"); ;
        }

        if (m_pTextCtrl->GetInsertionPoint() == m_pTextCtrl->GetLastPosition())
        {
            ch = _T('\0');
        }

        else
        {
            ch = m_pTextCtrl->GetValue[m_pTextCtrl->GetInsertionPoint()];
        }

        switch(ch)
        {
            case _T('\n'):
            {
                htmlString << _T("\n<br>");
            }
            break;

            case _T('<'):
            {
                htmlString << _T("&lt;");
            }
            break;

            case _T('&'):
            {
                htmlString << _T("&amp;");
            }
            break;

            case _T(' '):
            {
                htmlString << _T("&nbsp;");
            }
            break;

            default:
            {
                htmlString << ch;
            }
            break;
        }

        oldStyle = style;
    }

    htmlString << _T("</font></tt></body></html>");

    return htmlString;
}

I get build errors though...

Code
Switching to target: default
Compiling: main.cpp
main.cpp: In member function `wxString MainFrame::TextToHTML()':
main.cpp:214: error: no match for 'operator!=' in 'style != oldStyle'
C:/wxWidgets-2.6.2/include/wx/string.h:1428: note: candidates are: bool operator!=(const wxString&, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1430: note:                 bool operator!=(const wxString&, const wxChar*)
C:/wxWidgets-2.6.2/include/wx/string.h:1432: note:                 bool operator!=(const wxChar*, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1473: note:                 bool operator!=(const wxString&, const wxCharBuffer&)
C:/wxWidgets-2.6.2/include/wx/string.h:1475: note:                 bool operator!=(const wxCharBuffer&, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1496: note:                 bool operator!=(wxChar, const wxString&)
C:/wxWidgets-2.6.2/include/wx/string.h:1497: note:                 bool operator!=(const wxString&, wxChar)
C:/wxWidgets-2.6.2/include/wx/longlong.h:917: note:                 bool operator!=(long int, const wxLongLong&)
C:/wxWidgets-2.6.2/include/wx/longlong.h:930: note:                 bool operator!=(long unsigned int, const wxULongLong&)
C:/Program Files/CodeBlocks/include/objbase.h:82: note:                 BOOL operator!=(const GUID&, const GUID&)
main.cpp:239: error: invalid types `<unknown type>[long int]' for array subscript
Process terminated with status 1 (0 minutes, 11 seconds)
 


What am I doing wrong?

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Compile errors
« Reply #1 on: November 07, 2005, 03:48:19 am »
It seems like there's no operator != overloaded for wxTextAttr, so if(style != oldStyle) cannot be compiled.

sethjackson

  • Guest
Re: Compile errors
« Reply #2 on: November 07, 2005, 02:24:07 pm »
but what about this.

Code
error: invalid types `<unknown type>[long int]' for array subscript

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: Compile errors
« Reply #3 on: November 07, 2005, 03:00:27 pm »
but what about this.

Code
error: invalid types `<unknown type>[long int]' for array subscript

Change ch = m_pTextCtrl->GetValue[m_pTextCtrl->GetInsertionPoint()];
to ch = m_pTextCtrl->GetValue()[m_pTextCtrl->GetInsertionPoint()];

(notice the parentheses)
Be patient!
This bug will be fixed soon...

sethjackson

  • Guest
Re: Compile errors
« Reply #4 on: November 07, 2005, 04:23:39 pm »
I tried that. I still get errors

Code
main.cpp:235: error: ambiguous overload for 'operator[]' in '*(((MainFrame*)this)->MainFrame::m_pTextCtrl->wxTextCtrl::_vptr$wxObject + 768u)()[(*(((MainFrame*)this)->MainFrame::m_pTextCtrl->wxTextCtrl::_vptr$wxObject + 936u))(((MainFrame*)this)->MainFrame::m_pTextCtrl)]'
main.cpp:235: note: candidates are: operator[](const wxChar*, int) <built-in>
C:/wxWidgets-2.6.2/include/wx/string.h:767: note:                 wxChar& wxString::operator[](int)
C:/wxWidgets-2.6.2/include/wx/string.h:769: note:                 wxChar& wxString::operator[](size_t)
Process terminated with status 1 (0 minutes, 13 seconds)


I got

Code
m_pTextCtrl->GetValue[m_pTextCtrl->GetInsertionPoint()];

From the wxWidgets manual so it should work I thought....

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Compile errors
« Reply #5 on: November 07, 2005, 04:34:05 pm »
Well, that error happened so many times to me, but it was with an older version of wxWidgets IIRC.

The problem is operator [] for wxString is overloaded for both int and size_t, and GetInsertionPoint() returns long. The compiler doesn't know if it should convert that long to int or to size_t, then the ambiguity.

Try this:

Code
m_pTextCtrl->GetValue()[static_cast<int>(m_pTextCtrl->GetInsertionPoint())];

If that doesn't help, then try:

Code
int insertionPoint = m_pTextCtrl->GetInsertionPoint();
m_pTextCtrl->GetValue()[insertionPoint];

sethjackson

  • Guest
Re: Compile errors
« Reply #6 on: November 07, 2005, 04:50:15 pm »
Ok I get it now. Here is my code. Compiles with no erros now. Thanks Ceniza and mandrav. :)

Code
wxString MainFrame::TextToHTML()
{
    const wxString sOO = "00";
    const wxString s8O = "80";
    const wxString sFF = "FF";

    wxString htmlString;

    htmlString << "<html><head><title></title></head><body><tt>";

    long max = m_pTextCtrl->GetLastPosition();

    wxTextAttr style, oldStyle;

    wxChar ch;

    for(long pos = 0; pos < max; pos++)
    {
        m_pTextCtrl->GetStyle(pos, style);

       //if(style != oldStyle)
       //{
            if(pos > 0)
            {
                htmlString << "</font>";
            }

            wxString sR, sG, sB;

            wxColour colour = style.GetTextColour();

            sR.Printf("%02X", colour.Red());
            sG.Printf("%02X", colour.Green());
            sB.Printf("%02X", colour.Blue());

            htmlString << "<font color = \"#" << sR << sG << sB << "\">"; ;
        //}

        if (m_pTextCtrl->GetInsertionPoint() == max)
        {
            return htmlString;
        }

        else
        {
            ch = m_pTextCtrl->GetValue()[static_cast<int>(pos)];
        }

        switch(ch)
        {
            case '\n':
            {
                htmlString << "\n<br>";
            }
            break;

            case '<':
            {
                htmlString << "&lt;";
            }
            break;

            case '&':
            {
                htmlString << "&amp;";
            }
            break;

            case ' ':
            {
                htmlString << "&nbsp;";
            }
            break;

            default:
            {
                htmlString << ch;
            }
            break;
        }

        oldStyle = style;
    }

    htmlString << "</font></tt></body></html>";

    return htmlString;
}


One more question. :)

Is there any way to compare wxTextAttr's? I need to check if one is different than the other. What is the best way of doing this?
See the commented out part. :)

sethjackson

  • Guest
Re: Compile errors
« Reply #7 on: November 07, 2005, 09:28:21 pm »
Ah nevermind here is the code. :)

Code
wxString MainFrame::TextToHTML()
{
    wxString htmlString;

    htmlString << "<html><head><title></title></head><body>";

    long max = m_pTextCtrl->GetLastPosition();

    wxTextAttr style, oldStyle;

    wxChar ch;

    for(long pos = 0; pos < max; pos++)
    {
        ch = m_pTextCtrl->GetValue()[static_cast<int>(pos)];

        switch(ch)
        {
            case '\n':
            {
                htmlString << "<br />";
            }
            break;

            case '<':
            {
                htmlString << "&lt;";
            }
            break;

            case '&':
            {
                htmlString << "&amp;";
            }
            break;

            case ' ':
            {
                htmlString << "&nbsp;";
            }
            break;

            default:
            {
                htmlString << ch;
            }
            break;
        }

        oldStyle = style;
    }

    htmlString << "</body></html>";

    return htmlString;
}

« Last Edit: November 07, 2005, 09:46:42 pm by sethjackson »