unsigned longitud;
cin >> longitud;
    vector<char> palabra(longitud);
    for (unsigned i = 0; i < palabra.size(); i++, letra += 1)
    {
        palabra[i].push_back ('a') << " ";
        }

The error i get:

error: request for member `push_back' in `(&palabra)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = char, _Alloc = std::allocator<char>](i)', which is of non-class type `char'

I don't even understand what this error is saying :-/

Recommended Answers

All 4 Replies

Line 4 is initializing the vector to have longitud number of elements. Each push_back() will add another item to the end of the vector, which will increase the vector size.

You don't want to use push_back() if you initialize the vector like that. Just use [] operator

for (unsigned i = 0; i < palabra.size(); i++, letra += 1)
{
       palabra[i] = 'a';
}

Also, line 7 is wrong because vector does not have an overloaded << operator. So "<< " " makes no sense.

unsigned longitud;
cin >> longitud;
    vector<char> palabra(longitud);
    for (unsigned i = 0; i < palabra.size(); i++, letra += 1)
    {
        palabra[i].push_back ('a') << " ";
        }

The error i get:

error: request for member `push_back' in `(&palabra)->std::vector<_Tp, _Alloc>::operator[] [with _Tp = char, _Alloc = std::allocator<char>](i)', which is of non-class type `char'

I don't even understand what this error is saying :-/

Your problem is with the way you're using the vector with the push_back function.
push_back is used to put an item onto the back of the vector, but you can't say at item in the vector push a value to the back of the vector.
The following code:

palabra.push_back('a');

would basically find the last empty element in the vector (or if there was not enough space, the vector would reassign itself some more memory and grow some extra spaces ). It then pushes the item ('a') into the last element of the vector.

When trying to index an element of a vector and assign it a value, you can use an iterator or a standard array-style index to reference an item in the vector and assign it a value.
So from looking at your code, I think what you're trying to do is this:

palabra[i]='a';  // NOTE: the << " "; part would also throw an error so that's been removed!

You should also note that 'letra' has not been declared/defined correctly in your snippet...If you have it declared elsewhere in your code, then you don't need to worry about it. Otherwise you should make sure that you declare it properly!

Cheers for now,
Jas.

Line 4 is initializing the vector to have longitud number of elements. Each push_back() will add another item to the end of the vector, which will increase the vector size.

You don't want to use push_back() if you initialize the vector like that. Just use [] operator

for (unsigned i = 0; i < palabra.size(); i++, letra += 1)
{
       palabra[i] = 'a';
}

Also, line 7 is wrong because vector does not have an overloaded << operator. So "<< " " makes no sense.

Doh, the dragon got in there while I was composing my rambling post! heh heh! :)

First of all i should have posted all the code, but i didn't cause it seemed irrelevant. But everything was declared and the compiler agreed with that.

Line 4 is initializing the vector to have longitud number of elements. Each push_back() will add another item to the end of the vector, which will increase the vector size.

You don't want to use push_back() if you initialize the vector like that. Just use [] operator

Also, line 7 is wrong because vector does not have an overloaded << operator. So "<< " " makes no sense.

Ok, yes, that makes sense. I forgot what push_back() exactly does.

The << " " is because i had a "cout" at the beginning, i erased it just to test its influence to the error but i forgot that piece of code in the way.

Your problem is with the way you're using the vector with the push_back function.
push_back is used to put an item onto the back of the vector, but you can't say at item in the vector push a value to the back of the vector.
The following code:

palabra.push_back('a');

would basically find the last empty element in the vector (or if there was not enough space, the vector would reassign itself some more memory and grow some extra spaces ). It then pushes the item ('a') into the last element of the vector.

When trying to index an element of a vector and assign it a value, you can use an iterator or a standard array-style index to reference an item in the vector and assign it a value.
So from looking at your code, I think what you're trying to do is this:

palabra[i]='a';  // NOTE: the << " "; part would also throw an error so that's been removed!

You should also note that 'letra' has not been declared/defined correctly in your snippet...If you have it declared elsewhere in your code, then you don't need to worry about it. Otherwise you should make sure that you declare it properly!

Cheers for now,
Jas.

Thank you so much, i was really stuck in this one!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.