vector::push_back conversion from const Type* to type*

Day_Dreamer

The signature of vector::push_back is:

void push_back (const value_type& val);

which means it is responsible for not corrupting val.

My function is:

Result User::addFriend(const User* newFriend)
{
    // check that newFriend is valid and is not already a friend
    if (newFriend == NULL || isFriend(newFriend)) return FAILURE;
    friends_.push_back(newFriend);
    friendsNum_++;
    return SUCCESS;
}

when I compile the .c file I get compilation error:

invalid conversion from 'const User*' to 
std::vector User*::value_type
  1. Why do we get this error if push_back promises not to change val?

  2. What is the correct and clean way to overcome the error? maybe by const_cast?

thank you!

User.h:

class User {
...
public:
    vector<User*> friends_;
...
}
Barry

There is a logical issue with your code. Namely, you are storing pointers to modifiable objects:

vector<User*> friends_;

And yet your addFriend() method takes a pointer to a const object:

Result User::addFriend(const User* newFriend) 
//                     ^^^^^
{
    // stuff..
    friends_.push_back(newFriend);
}

Those don't line up. As is, push_back would have to convert your const User* to a User*, hence the compile error. One or the other of those types should be changed. Either friends_ needs to be a vector<const User*> (if you really have no intention of modifying these objects) or addFriend needs to take a User* (if you do). Don't use const_cast!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ invalid conversion from ‘const type* const’ to ‘type*’

From Dev

C++11 member variable of reference type, different behaviour after vector push_back

From Dev

no viable conversion from returned value of type const_iterator to iterator

From Dev

no conversion from "std::allocator" to "const allocator_type"

From Dev

Why can't I push_back to a vector of const elements?

From Dev

implicit conversion of vector from one type to another c++

From Dev

How to enable implicit type conversion from non-const to const in C++?

From Java

Is it safe to push_back an element from the same vector?

From Dev

conversion from ‘std::vector<AdjacencyData> (*)()’ to non-scalar type ‘std::vector<AdjacencyData>’ requested

From Dev

Automatic type conversion for templates with const/non-const pointer types

From Dev

"Conversion" from type to same type is causing error

From Dev

Conversion from type 'DBNull' to type 'String' is not valid

From Dev

Conversion from type 'DBNull' to type 'Double' is not valid

From Dev

Conversion from type 'Button' to type string is not valid

From Dev

Conversion from type 'DBNull' to type 'String' is not valid

From Dev

Conversion from type 'Button' to type string is not valid

From Dev

Type conversion from phparray to jsonarray and sending back in response

From Dev

Type conversion from phparray to jsonarray and sending back in response

From Dev

conversion from `const char[2]' to non-scalar type `Persona' requested

From Dev

Conversion from string to generic type

From Dev

std vector push_back' : 2 overloads have no legal conversion for 'this' pointer error

From Dev

vector push_back in STL?

From Dev

Vector and push_back() behavior

From Dev

Unable to push_back to vector

From Dev

Type conversion

From Dev

error: no viable conversion from 'tuple<[...], std::__1::tuple<unsigned long long, unsigned long long>>' to 'const tuple<[...], uint_type>'

From Dev

vector push_back zero into an empty vector

From Dev

push_back a vector of vectors into a vector

From Dev

auto it = vector.begin() resulting type is not convertible to const_iterator

Related Related

  1. 1

    C++ invalid conversion from ‘const type* const’ to ‘type*’

  2. 2

    C++11 member variable of reference type, different behaviour after vector push_back

  3. 3

    no viable conversion from returned value of type const_iterator to iterator

  4. 4

    no conversion from "std::allocator" to "const allocator_type"

  5. 5

    Why can't I push_back to a vector of const elements?

  6. 6

    implicit conversion of vector from one type to another c++

  7. 7

    How to enable implicit type conversion from non-const to const in C++?

  8. 8

    Is it safe to push_back an element from the same vector?

  9. 9

    conversion from ‘std::vector<AdjacencyData> (*)()’ to non-scalar type ‘std::vector<AdjacencyData>’ requested

  10. 10

    Automatic type conversion for templates with const/non-const pointer types

  11. 11

    "Conversion" from type to same type is causing error

  12. 12

    Conversion from type 'DBNull' to type 'String' is not valid

  13. 13

    Conversion from type 'DBNull' to type 'Double' is not valid

  14. 14

    Conversion from type 'Button' to type string is not valid

  15. 15

    Conversion from type 'DBNull' to type 'String' is not valid

  16. 16

    Conversion from type 'Button' to type string is not valid

  17. 17

    Type conversion from phparray to jsonarray and sending back in response

  18. 18

    Type conversion from phparray to jsonarray and sending back in response

  19. 19

    conversion from `const char[2]' to non-scalar type `Persona' requested

  20. 20

    Conversion from string to generic type

  21. 21

    std vector push_back' : 2 overloads have no legal conversion for 'this' pointer error

  22. 22

    vector push_back in STL?

  23. 23

    Vector and push_back() behavior

  24. 24

    Unable to push_back to vector

  25. 25

    Type conversion

  26. 26

    error: no viable conversion from 'tuple<[...], std::__1::tuple<unsigned long long, unsigned long long>>' to 'const tuple<[...], uint_type>'

  27. 27

    vector push_back zero into an empty vector

  28. 28

    push_back a vector of vectors into a vector

  29. 29

    auto it = vector.begin() resulting type is not convertible to const_iterator

HotTag

Archive