Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 23-03-2009
Member
 
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 &'
Reply With Quote
  #2  
Old 23-03-2009
XSI XSI is offline
Member
 
Join Date: May 2008
Posts: 271
Re: C++ error C2662

get is not const
Reply With Quote
  #3  
Old 23-03-2009
Member
 
Join Date: May 2008
Posts: 685
Re: C++ error C2662

Can you explain me why are you using "friend"?
Reply With Quote
  #4  
Old 23-03-2009
Member
 
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)); 
}
Reply With Quote
  #5  
Old 23-03-2009
Member
 
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?
Reply With Quote
  #6  
Old 23-03-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "C++ error C2662"
Thread Thread Starter Forum Replies Last Post
Getting Error message ?Northbridge Error, node 2<0>K8 ECC error.? in CentOS 5.5 Pawna Operating Systems 5 04-05-2011 10:50 AM
error “shmget (lowmem) error: Unknown error 999” on linux -Wamil- Operating Systems 6 12-11-2010 11:37 PM
Server Error: 451, Socket Error: 10053, Error Number: 0x800CCC0F Eigenberg Windows XP Support 3 03-06-2008 04:13 PM
Error displaying page error [Error number: 0x80090305] shai Windows Update 3 21-07-2005 01:07 AM


All times are GMT +5.5. The time now is 01:30 AM.