0

This is likely very simple. I have added an std::set<int> my_set; to my header file for some class. Then, in that classes implementation, I try to insert into this set. As an example, just doing my_set.insert(1); This is not compiling, which is very strange behaviour. Here is my compiler error:

error C2663: 'std::_Tree<_Traits>::insert' : 4 overloads have no legal conversion for 'this' pointer

The file I included to use set is #include <set>. What have I done wrong? I have not called the set anywhere else in the code.

Jim
  • 4,509
  • 16
  • 50
  • 80
  • I think more coded is needed. Where do you do this insertion? A member function? – juanchopanza May 17 '12 at 20:04
  • 5
    Are you trying to insert from a `const` member function, or on a `const` object? Show your actual code. – ildjarn May 17 '12 at 20:05
  • I think @ildjarn guessed it a `const` method would give this error as `insert` is non-const so it can't find a valid overload. – AJG85 May 17 '12 at 20:08
  • 1
    Also, judging by the message, you copied that from Visual Studio's Error Window. The error window only holds summaries of errors. The full Error Text is in the "Output" window, and will probably provide a _lot_ of useful information. – Mooing Duck May 17 '12 at 20:13

1 Answers1

3

Since you're getting an error about this, but you're trying to insert an int, I'm gonna go out on a limb here and guess you're trying to do this in a const method.

class A
{
    std::set<int> my_set;
    void foo() 
    {
       my_set.insert(1); //OK
    }
    void foo() const
    {
       my_set.insert(1); //not OK, you're trying to modify a member
                         //std::set::insert is not const, so you can't call it
    }
};

Remove the const. It seems like the logical thing to do, since you're modifying members.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625