+ -
当前位置:首页 → 问答吧 → CString转换为LPCTSTR问题

CString转换为LPCTSTR问题

时间:2011-12-05

来源:互联网

C/C++ code
#include "iostream"
#define _AFXDLL
#include "afx.h"
#pragma comment(linker,"/ENTRY:TEST")
using namespace std;
int TEST() {
    LPCTSTR xx = CString("asdfas");
    cout << xx << endl;
    cin.get();
    return 0;
}

这段代码错在哪里啊?谢谢回答 。

作者: ereere235   发布时间: 2011-12-05

没有错误啊!
========== 生成: 成功 1 个,失败 0 个,最新 0 个,跳过 0 个 ==========

作者: tony2278   发布时间: 2011-12-05

C/C++ code
// test.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include "iostream"
#define _AFXDLL
#include "afx.h"
//#pragma comment(linker,"/ENTRY:TEST")
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
        LPCTSTR xx = CString("asdfas");
    cout << xx << endl;
    cin.get();
    return 0;
}

作者: tony2278   发布时间: 2011-12-05

如果说有错误,那就是
C/C++ code
LPCTSTR xx = CString("asdfas"); //这里转换可能会导致和想要的不一样!

作者: tony2278   发布时间: 2011-12-05

我用的vs2010啊, 出错了, 错误 是
CPP.exe 中的 0x1027d540 (msvcr100d.dll) 处有未经处理的异常: 0xC0000005: 读取位置 0x0039c000 时发生访问冲突

怎么回事啊。。

作者: ereere235   发布时间: 2011-12-05

在我这里,用vs2008没问题啊!是不是你使用的动态库有问题啊!

作者: tony2278   发布时间: 2011-12-05

啥 意思啊?

作者: ereere235   发布时间: 2011-12-05

这么短都有编译错误?那简单,一句一句的屏蔽试试。

作者: wshjldaxiong   发布时间: 2011-12-05

LPCTSTR是一个指针的类型 cout << xx << endl 也就只能把其地址打印出来。

作者: xiaoyuetian   发布时间: 2011-12-05

到cout那句出的错,输出xx的时候。

作者: ereere235   发布时间: 2011-12-05

C/C++ code
CString x("afdads");
    LPCTSTR xx = x;
    cout << xx << endl;


这样写倒是没出错。。不明白啊。

作者: ereere235   发布时间: 2011-12-05