我想将自定义类转换为在VS2005中编译的字符串。但在VS2012中,我收到编译器错误error C2440: 'type cast' : cannot convert from 'A' to 'std::string'
。我需要改变什么?这是我的例子:如何将自定义类型转换为VS2012中的字符串?
#include <string>
using namespace std;
class A
{
public:
A& operator=(const char* c);
operator string();
operator const char*();
private:
string value;
};
A::operator string() { return string((const char*)(*this)); }
A& A::operator = (const char* aValue) { value = aValue; return *this; }
A::operator const char *() { const char* wort = "Hello"; return wort; }
int main()
{
A a;
string s = (string)a; // C2440
}
这工作好wth'VS2010'。 你可以看看这个答案,可能你会得到一些线索。 http://stackoverflow.com/questions/3558589/error-c2440-type-cast-cannot-convert-from-std-vector-iterator-ty-alloc – 2013-03-01 15:03:21