invalid conversion from ‘const char*’ to ‘std::vector<char*>

I try to implement a constructor for a class Dictionary, but compiler gives an error which I don't understand.

Here is a constructor:

1
2
3
4
5
6
7
8
9
 Dictionary::Dictionary(const std::vector<const char*> &list)
{
	int i,Size;
	vector<char*>* words = new vector <char*>;
	Size=(int)list.size();
	for(i=0;i<Size;i++)
		words->push_back(list[i]);	
	root=new Dict_Node();
}


Compiler error is:
In constructor ‘Dictionary::Dictionary(const std::vector<const char*>&)’:
dict.cpp:53:27: error: invalid conversion from ‘const char*’ to ‘std::vector<char*>::value_type {aka char*}’ [-fpermissive]
/usr/include/c++/4.6/bits/stl_vector.h:826:7: error:   initializing argument 1 of ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = char*, _Alloc = std::allocator<char*>, std::vector<_Tp, _Alloc>::value_type = char*]’ [-fpermissive]


What does it mean? Am I supposed to cast it somehow?
The compiler does noot allow to convert const char * to char *
Otherwise you could change const character arrays.
So I have to const_cast it.
You do realize that words is local to the Dictionary constructor, and the memory you allocate for it and the memory it allocates to hold your stuff is going to be leaked as soon as the constructor is left?

So I have to const_cast it.


Perhaps it would be better to just respect the const.
Last edited on
no, you do have to const-cast it,a and there is no error in it! As long as you don't change the const item, you can const_cast it as much as you want:
1
2
3
4
5
6
7
8
9
10
11
Dictionary::Dictionary(const std::vector<const char*> &list)
{
	int i,Size;
	vector<char*>* words = new vector <char*>;
	Size=(int)list.size();
	for(i=0;i<Size;i++)
		words->push_back(const_cast<char*>(list[i])); // no error, not changing the item
                                                              //just casting it to match the other side of the equation
                                                              //(happening inside the push_back() function)
	root=new Dict_Node();
}
A better solution is to make the new vector contain const char* or, even better, strings.
Last edited on
Telion do you mean like this

1
2
3
4
5
6
7
8
9
10
11
Dictionary::Dictionary(const std::vector<const char*> &list)
{
	int i,Size;
	vector<const char*>* words = new vector <const char*>;
	Size=(int)list.size();
	for(i=0;i<Size;i++)
		words->push_back(list[i]); // no error, not changing the item
                                                              //just casting it to match the other side of the equation
                                                              //(happening inside the push_back() function)
	root=new Dict_Node();
}


please correct me if i am wrong .. .
Last edited on
Yes, that is correct. You shouldn't ever try to hold data that was originally const char* in a char* container, since it might be illegally changed in the future. Now that the vector holds const char*, the compiler will stop you from making illegal changes.
closed account (zb0S216C)
john andre wrote:
vector<const char*>* words = new vector <const char*>;

My eyes are offended. It's best to use the std::vector the way it was supposed to be used.

Edit:

Use std::strings:

1
2
3
4
5
6
7
void Copy(const std::vector<const char *> &Vector)
{
    std::vector<std::string> NewVector;
    std::vector<const char *>::const_iterator MyItor(Vector.begin());
    for( ; MyItor < Vector.end(); ++MyItor)
        NewVector.push_back(*MyItor);
}


Another Edit:

Or, take the unsafe path:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Copy(const std::vector<const char *> &Vector)
{
    union
    {
        char *Str;
        const char *ConstStr;
    } Convert;

    std::vector<char *> NewVector;
    std::vector<const char *>::const_iterator MyItor(Vector.begin());

    Convert.ConstStr = *MyItor;

    for(MyItor = Vector.begin(); MyItor < Vector.end(); ++MyItor)
        NewVector.push_back(Convert.Str);
}

Wazzak
Last edited on
Topic archived. No new replies allowed.