cannot convert from ???? to ???

baiyuxiong 2009-09-01 02:23:59
一遇到类型转换的问题就很头大
cannot convert from ???? to ???
大家有没有好的经验分享一下?
...全文
202 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
baiyuxiong 2009-09-01
够雷,哈哈
回复
songsu 2009-09-01
我有个雷类。
// Convert.cpp: implementation of the Convert class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Convert.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

using namespace Xproer::System;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Convert::Convert()
{

}

Convert::~Convert()
{

}

int Convert::ToInt(wchar_t* v)
{
string cs = ToString(v);

return ToInt(cs);
}

int Convert::ToInt(const char* v)
{
return atoi(v);
}

int Convert::ToInt(char* v)
{
return atoi(v);
}

/*
* 2008-12-15 将 v.GetBuffer 转换改为 (LPCTSTR)
*/
int Convert::ToInt(const CString& v)
{
return atoi((LPCTSTR)v);
}

int Convert::ToInt(const string& v)
{
return ToInt(v.c_str());
}

int Convert::ToInt(const wstring& v)
{
wchar_t* wcs = const_cast<wchar_t*>(v.c_str());
return ToInt(wcs);
}

/*
* To Int64
*/
__int64 Convert::ToInt64(char* v)
{
return _atoi64(v);
}

__int64 Convert::ToInt64(const char* v)
{
return _atoi64(v);
}

/*
* 2008-12-15 将 v.GetBuffer 转换改为 (LPCTSTR)
*/
__int64 Convert::ToInt64(const CString& v)
{
return ToInt64((LPCTSTR)v);
}

__int64 Convert::ToInt64(const string& v)
{
return ToInt64(v.c_str());
}

__int64 Convert::ToInt64(const wstring& v)
{
return ToInt64(ToString(v));
}

/*
* To Long
*/
long Convert::ToLong(char* v)
{
return atol(v);
}

long Convert::ToLong(wchar_t* v)
{
string str = ToString(v);
return ToLong(str);
}

long Convert::ToLong(const char* v)
{
return atol(v);
}

/*
* 2005-12-15 将 v.GetBuffer 转换改为 (LPCTSTR)
*/
long Convert::ToLong(const CString& v)
{
return ToLong((LPCTSTR)v);
}

long Convert::ToLong(const string& v)
{
return ToLong(v.c_str());
}

long Convert::ToLong(const wstring& v)
{
long i = 0;
return i;
}

/*
* To unsigned long
*/
unsigned long Convert::ToUnLong(char* v)
{
char* end;
return strtoul(v,&end,10);
}

unsigned long Convert::ToUnLong(const char* v)
{
char* end;
return strtoul(v,&end,10);
}

unsigned long Convert::ToUnLong(wchar_t* v)
{
wchar_t* end;

return wcstoul(v,&end,10);
}

unsigned long Convert::ToUnLong(const string& v)
{
return ToUnLong(v.c_str());
}

unsigned long Convert::ToUnLong(const wstring& v)
{
string s = ToString(v);
return ToUnLong(s);
}

/*
* 2008-12-15 将 v.GetBuffer 转换改为 (LPCTSTR)
*/
unsigned long Convert::ToUnLong(const CString& v)
{
return ToUnLong((LPCTSTR)v);
}

/*
* To Double
*/
double Convert::ToDouble(const string& v)
{
return ToDouble(v.c_str());
}

double Convert::ToDouble(char* v)
{
char *end;

return strtol(v,&end,10);
}

double Convert::ToDouble(const char* v)
{
char *end;

return strtol(v,&end,10);
}

double Convert::ToDouble(wchar_t* v)
{
wchar_t* end;

return wcstod( v,&end);
}

/*
* 2008-12-15 将 v.GetBuffer 改为 (LPCTSTR)
*/
double Convert::ToDouble(const CString& v)
{
char* end;

return strtol((LPCTSTR)v,&end,10);
}

double Convert::ToDouble(const wstring& v)
{
string s = ToString(v);
return ToDouble(s);
}

/*
* To std::string
*/
string Convert::ToString(int v)
{
char *vs = new char[sizeof(int)];

itoa(v,vs,10);
string sv(vs);
return sv;
}

string Convert::ToString(float v)
{
char fs[sizeof(v)];

ZeroMemory(fs,sizeof(v));
sprintf(fs,"%f",v);
return ToString(fs);
}

string Convert::ToString(double v)
{
int decimal;
int sign;
char *buffer;
int precision = 10;

buffer = _ecvt( v, precision, &decimal, &sign);
return string(buffer);
}

string Convert::ToString(long v)
{
char lv[sizeof(v)];

_ltoa(v, lv, 10);
return string(lv);
}

string Convert::ToString(char* v)
{
string sv(v);
return sv;
}

string Convert::ToString(const char* v)
{
return string(v);
}

/*
* 2008-12-15 将 v.GetBuffer 改为 (LPCTSTR)
*/
string Convert::ToString(const CString& v)
{
return string((LPCTSTR)v);
}

string Convert::ToString(const wstring& v)
{
string cs;
return cs;
}

string Convert::ToString(wchar_t* v)
{
char cstr[sizeof(v)];

WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,v,-1,cstr,sizeof(CString),NULL,NULL);
return string(cstr);
}

/*
* To wstring
*/
wstring Convert::ToWString(int v)
{
wchar_t wv[sizeof(int)];

_itow(v, wv,10);
return wstring(wv);
}

wstring Convert::ToWString(long v)
{
wchar_t wv[sizeof(v)];

_ltow(v, wv, 10);
return wstring(wv);
}

wstring Convert::ToWString(unsigned long v)
{
wchar_t wc[sizeof(v)];

_ultow(v, wc, 10);
return ToWString(wc);
}

wstring Convert::ToWString(char* v)
{
wchar_t wschar[sizeof(v) * 2];

long len = MultiByteToWideChar(CP_ACP,0,v,strlen(v),wschar,sizeof(wschar));
wschar[len] = '\0';
return wstring(wschar);
}

wstring Convert::ToWString(const char* v)
{
return ToWString(v);
}

wstring Convert::ToWString(wchar_t* v)
{
return wstring(v);
}

wstring Convert::ToWString(const CString& v)
{
return wstring(v.AllocSysString());
}

wstring Convert::ToWString(const string& v)
{
wchar_t wcs[sizeof(v) * 2];

setlocale(LC_CTYPE, ""); //很重要,没有这一句,转换会失败
mbstowcs(wcs, v.c_str(),v.size());
return wstring(wcs);
}

/*
* UTF8 To WString
*/
wstring Convert::UTF8ToWString(char* utf8)
{
wchar_t wschar[sizeof(utf8) * 2];

long len = MultiByteToWideChar(CP_UTF8,0,utf8,strlen(utf8),wschar,sizeof(wschar));
wschar[len] = '\0';
wstring sv(wschar);
return sv;
}

wstring Convert::UTF7ToWString(char* utf7)
{
wchar_t wschar[sizeof(utf7) * 2];

long len = MultiByteToWideChar(CP_UTF7,0,utf7,strlen(utf7),wschar,sizeof(wschar));
wschar[len] = '\0';
wstring sv(wschar);
return sv;
}

/*
* To BSTR
*/
BSTR Convert::ToBSTR(char* v)
{
CString s = CString(v);
return s.AllocSysString();
}
BSTR Convert::ToBSTR(const char* v)
{
return ToBSTR(v);
}
BSTR Convert::ToBSTR(const CString& v)
{
return v.AllocSysString();
}
BSTR Convert::ToBSTR(const string& v)
{
return ToBSTR(v.c_str());
}
BSTR Convert::ToBSTR(const wstring& v)
{
string s = ToString(v);
return ToBSTR(s);
}
BSTR Convert::ToBSTR(wchar_t* v)
{
string s = ToString(v);
return ToBSTR(s);
}
BSTR Convert::ToBSTR(int v)
{
string s = ToString(v);
return ToBSTR(s);
}
BSTR Convert::ToBSTR(long v)
{
string s = ToString(v);
return ToBSTR(s);
}
BSTR Convert::ToBSTR(double v)
{
string s = ToString(v);
return ToBSTR(s);
}
BSTR Convert::ToBSTR(float v)
{
string s = ToString(v);
return ToBSTR(v);
}
回复
muzizongheng 2009-09-01
widebytetoMultibyte
回复
baiyuxiong 2009-09-01
from 'TCHAR *' to 'char *'

这个怎么转?
回复
MoXiaoRab 2009-09-01
我们都是Google的,遇到的次数多了,也就有经验了

把??? 和 ??拿到MSDN里面去查下,自己慢慢改改就熟了
回复
baiyuxiong 2009-09-01
每次都百度很烦,想问问大家有没有百试不爽的高招,呵呵

[Quote=引用 1 楼 taoshengyijiu20008 的回复:]
百度一下,问题迎刃而解
[/Quote]
回复
大熊猫侯佩 2009-09-01
这个没那么头疼吧,主要是debug的时候烦人
回复
岁月小龙 2009-09-01
百度一下,问题迎刃而解
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告