Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Operator Error Causing Vector BS (http://www.programmingforums.org/showthread.php?t=10595)

Kilo Jul 1st, 2006 9:40 PM

Operator Error Causing Vector BS
 
I still do understand why I can never get basic crap to work! So annoying...

:

        for ( unsigned int i = 0; i <= o_game->m_msgList.size( ); i++)
        {
                TextOut(hdc, 10, 400, o_game->m_msgList[i], sizeof ( o_game->m_msgList[i] ) );
        }



Error
:

------ Build started: Project: Delectria, Configuration: Debug Win32 ------
Compiling...
game.cpp
c:\documents and settings\k\my documents\visual studio 2005\projects\delectria\delectria\game.h(18) : error C3203: 'basic_string' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
c:\documents and settings\k\my documents\visual studio 2005\projects\delectria\delectria\game.cpp(15) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'std::string' to 'const int &'
        with
        [
            _Ty=int
        ]
        Reason: cannot convert from 'std::string' to 'const int'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Build log was saved at "file://c:\Documents and Settings\K\My Documents\Visual Studio 2005\Projects\Delectria\Delectria\Debug\BuildLog.htm"
Delectria - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


this is dumb!

EDIT:
Yes i understand the msg's that will be outputing will paint over each other i haven't done the math yet

Cache Jul 1st, 2006 10:02 PM

Main changes in bold:
:

for (size_t i = 0; i < o_game->m_msgList.size(); ++i)
{
        ::TextOut(hdc, 10, 400, o_game->m_msgList[i].c_str(),
                o_game->m_msgList[i].size() );
}


grumpy Jul 1st, 2006 10:06 PM

You haven't given enough information. I'm guessing m_msgList is an array (or maybe a container, such as a std::vector) of std::strings.

A few hints that might help (with this error, and other issues)

1) TextOut() in the win32 API expects to receive a C-style string (an array of char delimited by a zero byte) as it's fourth argument, not a std::string. If o_game->m_msgList[i] is a std::string, it needs to be converted to a C-style string. For example; o_game->m_msgList[i].c_str().

2) The 5th argument passed to TextOut() is the length of the string. The sizeof operator does not produce the length of a string (except in some special cases, but yours isn't one of them). If we assume (again) that o_game->m_msgList[i] is a std::string, one way of computing the length of the string is o_game->m_msgList[i].length().

3) If your loop is intended to work on all elements of the array, it will work from zero to o_game->m_msgListsize()-1. Your termination clause i <= o_game->m_msgList.size() does not achieve that; it needs to be changed to i < o_game->m_msgList.size()

You will usually find things easier if you put some effort into understanding what is going on before you write your code, rather than just throwing poorly thought-out code at the compiler and wondering why it complains.

Kilo Jul 2nd, 2006 1:52 PM

thanks a ton for your help! (both of u)


All times are GMT -5. The time now is 12:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC