Results 1 to 6 of 6

Thread: C++ error C2662

  1. #1
    Join Date
    Nov 2008
    Posts
    159

    C++ error C2662

    Code:
    # include <fstream> 
    # include <vector> 
    # include <iostream> 
    # include <sstream> 
    # include <iterator> 
    struct MyClass
    {
    MyClass (int m = 0): m_ (m) {}
    friend std::ostream & operator << (std::ostream & o, const MyClass & m); 
    std::string get () 
    {
    ss_ << m_; 
    return ss_.str();
    }
    private: 
    std::stringstream ss_; 
    int m_; 
    }; 
    std::ostream & operator << (std::ostream & o, const MyClass & m) 
    {
    o <<m.get (); 
    return o; 
    }
    void main () 
    {
    std::vector <MyClass> Tab1; 
    MyClass z; 
    Tab1.push_back (z); 
    std::ostream_iterator <MyClass> (cout) output; 
    std::copy (Tab1.begin (), Tab1.end (), output); 
    system ("pause"); 
    }
    Why is this code does not work? The compiler tells me:

    error C2662: 'MyClass::get': can not convert a pointer 'this' from 'const MyClass' to 'MyClass &'

  2. #2
    Join Date
    May 2008
    Posts
    271

    Re: C++ error C2662

    get is not const

  3. #3
    Join Date
    May 2008
    Posts
    685

    Re: C++ error C2662

    Can you explain me why are you using "friend"?

  4. #4
    Join Date
    Nov 2008
    Posts
    159

    Re: C++ error C2662

    @ XSI
    And when I get const, it takes me to another problem, I change the stringstream, so I must declare mutable? if so I have yet another problem on copy constructor and = which are not accessible, I try to encapsulate it in a auto_ptr, but it does not work better.


    @ fellah

    why friend, yes I know that the use of friend is not often a good idea, then it's just that I recall most of the syntax that allows to do without them

    With the code below it works, but no more simple/clean

    Code:
    # include <vector> 
    # include <iostream> 
    # include <sstream> 
    # include <iterator> 
    using std::cout; 
    using std::stringstream; 
    using std::ostream; 
    using std::swap; 
    template <typename T> struct ValueInit; 
    template <> struct ValueInit<int>  { static int Init() { return 0;}}; 
    template <> struct ValueInit<std::string> { static std::string Init() { return "";}};
    template <typename T> 
    struct MyClass
    {
    explicit MyClass (T m = ValueInit <T>::Init ()): 
    m_(m)
    , ss_(std::auto_ptr <stringstream> (new stringstream ())){} 
    MyClass (const MyClass & m): m_ (m.m_) ss_ (m.ss_) {}
    void Swap (MyClass & mwc) 
    {
    std::swap (ss_, mwc.ss_); 
    std::swap (m_, mwc.m_); 
    }
    MyClass & operator = (MyClass m) 
    {
    Swap (m); 
    return * this; 
    }
    void print (ostream & o) const 
    {
    o <<get (); 
    }
    std:: string get () const 
    {
    * ss_ <<m_; 
    return ss_-> str (); 
    }
    private: 
    mutable std:: auto_ptr <stringstream> ss_; 
    T m_; 
    }; 
    template <class T> 
    std::ostream & operator <<(std::ostream & o, const MyClass <T> & m) 
    {
    m.print (o); 
    return o; 
    }
    void main () 
    {
    typedef MyClass <int> MYI 
    MYI z (9); 
    std::vector <MYI> Tab1; 
    Tab1.push_back (z); 
    std::copy (Tab1.begin (), Tab1.end (), std::ostream_iterator <MYI> (cout)); 
    }

  5. #5
    Join Date
    May 2008
    Posts
    685

    Re: C++ error C2662

    Code:
    void Swap (const MyClass<T> &m) 
    {
    MyClass <T> mwc = *const_cast <MyClass<T>*> (&m); 
    Swap (m_, mwc.m_); 
    Swap (ss_, mwc.ss_); 
    }
    you do not feel to do anything there?

  6. #6
    Join Date
    May 2008
    Posts
    3,971

    Re: C++ error C2662

    This error can be caused by invoking a non-const member function on a const object. Possible resolutions:

    1. Remove the const from the object declaration.
    2. Add const to the member function.

Similar Threads

  1. Replies: 5
    Last Post: 04-05-2011, 10:50 AM
  2. Replies: 6
    Last Post: 12-11-2010, 11:37 PM
  3. Server Error: 451, Socket Error: 10053, Error Number: 0x800CCC0F
    By Eigenberg in forum Windows XP Support
    Replies: 3
    Last Post: 03-06-2008, 04:13 PM
  4. Replies: 3
    Last Post: 21-07-2005, 01:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,518,380.19498 seconds with 16 queries