-
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 &'
-
Re: C++ error C2662
-
Re: C++ error C2662
Can you explain me why are you using "friend"?
-
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));
}
-
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?
-
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.
Page generated in 1,717,383,060.54532 seconds with 10 queries