vector and wstring remove problem

T

Thomas Mathew

Dear All,

I am trying to remove an entity from the vector but getting many error
messages. I know it is a casting problem but I don’t know how to fix
it.


Following is the code snippet.

bool CMdlMain::RemoveLayers()
{
bool result = false;
long index = m_oLayerListBox.Selected();

if (-1 < index) {
m_vecLayerList.erase (std::remove (m_vecLayerList.begin (),
m_vecLayerList.end (), m_vecLayerList[index]), m_vecLayerList.end
());
result = true;
}
return result ;
}

// Following is the vector declaration

typedef std::vector<std::wstring> Vector_Layers;
Vector_Layers m_vecLayerList;

// Following is the error

C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\algorithm(43) :
error C2784: 'bool __cdecl std::eek:perator ==(const class
std::istream_iterator<_U,_E,_Tr> &,const class
std::istream_iterator<_U,_E,_Tr> &)' : could not deduce template
argument

fo r 'const class std::istream_iterator<_U,_E,_Tr> &' from 'class
std::basic_string<unsigned short,struct std::char_traits<unsigned
short>,class std::allocator<unsigned short> >'


It would be a great help if anyone could kindly help me.


Thomas
 
A

Alf P. Steinbach

* Thomas Mathew:
I am trying to remove an entity from the vector but getting many error
messages. I know it is a casting problem but I don’t know how to fix
it.


Following is the code snippet.

bool CMdlMain::RemoveLayers()
{
bool result = false;
long index = m_oLayerListBox.Selected();

if (-1 < index) {
m_vecLayerList.erase (std::remove (m_vecLayerList.begin (),
m_vecLayerList.end (), m_vecLayerList[index]), m_vecLayerList.end
());
result = true;
}
return result ;
}

// Following is the vector declaration

typedef std::vector<std::wstring> Vector_Layers;
Vector_Layers m_vecLayerList;

// Following is the error

C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\algorithm(43) :
error C2784: 'bool __cdecl std::eek:perator ==(const class
std::istream_iterator<_U,_E,_Tr> &,const class
std::istream_iterator<_U,_E,_Tr> &)' : could not deduce template
argument

fo r 'const class std::istream_iterator<_U,_E,_Tr> &' from 'class
std::basic_string<unsigned short,struct std::char_traits<unsigned
short>,class std::allocator<unsigned short> >'


It would be a great help if anyone could kindly help me.

The code formatting leaves much to be desired, but the following
compiles fine:

#include <vector>
#include <string>
#include <algorithm>

struct CMdlMain
{
typedef std::vector<std::wstring> Vector_Layers;
Vector_Layers m_vecLayerList;

bool RemoveLayers();
};

bool CMdlMain::RemoveLayers()
{
bool result = false;
long index = 3; //m_oLayerListBox.Selected();

if (-1 < index) {
m_vecLayerList.erase (std::remove (m_vecLayerList.begin (),
m_vecLayerList.end (), m_vecLayerList[index]), m_vecLayerList.end
());
result = true;
}
return result ;
}

int main() {}

It seems you have not posted the code that gave you problems.

Check out the FAQ's advice on how to get help with Code That Does Not
Work (this includes posting a complete, small program demonstrating the
problem).


Cheers & hth.,

- Alf
 
P

Pete Becker

Dear All,

I am trying to remove an entity from the vector but getting many error
messages. I know it is a casting problem but I don’t know how to fix
it.


Following is the code snippet.

The code is far more complicated than it needs to be. If you've got an
index into a vector, you don't need to search for the corresponding
element. Just erase(begin() + index).
 
T

Thomas Mathew

Alf and Pete... Thanks for your valuable reply.

erase(begin() + index) is working fine.

Thanks Alf, Pete

Cheers, Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top