出现error C2664: “textout”: 不能将参数 6 从“const char [17]”转换为“LPTSTR”怎样修改程序?

cwchenwei2000 2009-08-31 12:02:48
我用VS 2005出现错误error C2664: “textout”: 不能将参数 6 从“const char [17]”转换为“LPTSTR”
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
请高手帮忙改改
文件ColorConsole.h
#include <windows.h>
#include <iostream>
using namespace std;

HANDLE initiate();
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString);
文件colorConsole.cpp
#include "colorConsole.h"
HANDLE initiate()
{
HANDLE hOutput;
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
return hOutput;
}
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString)
{
DWORD cWritten;
BOOL fSuccess;
COORD coord;

coord.X = x; // start at first cell
coord.Y = y; // of first row
fSuccess = WriteConsoleOutputCharacter(
hOutput, // screen buffer handle
lpszString, // pointer to source string
lstrlen(lpszString), // length of string
coord, // first cell to write to
&cWritten); // actual number written
if (! fSuccess)
cout<<"error:WriteConsoleOutputCharacter"<<endl;

for (;fSuccess && coord.X < lstrlen(lpszString)+x; coord.X += nColors)
{
fSuccess = WriteConsoleOutputAttribute(
hOutput, // screen buffer handle
wColors, // pointer to source string
nColors, // length of string
coord, // first cell to write to
&cWritten); // actual number written
}
if (! fSuccess)
cout<<"error:WriteConsoleOutputAttribute"<<endl;
return 0;
}
主文件 main.cpp
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include "colorConsole.h"
//投筛子
void rolldice(HANDLE hOutput,int n,int col,int row,WORD wColors[]);
void main(void)
{
HANDLE handle;
WORD wColors[1];
int row,col;
//初始化
handle = initiate();
//生成6个不同骰子
wColors[0]=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY;
row=col=2;
for (int i=0;i<6;i++)
rolldice(handle,i+1,col,row+6*i,wColors);
//打印屏幕底部菜单
WORD wMenuColors[1];
wMenuColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;
textout(handle,1,24,wMenuColors,1, "游戏规则:");
textout(handle,11,24,wMenuColors,1,"投掷开始/结束=ENTER;");
textout(handle,34,24,wMenuColors,1,"更换游戏者=空格;");
textout(handle,53,24,wMenuColors,1,"退出=q.");
bool flag=false;
int count=1;
int sum=0;
//随机数的种子
srand( (unsigned)time( NULL ));
col=15;
row=8;
//游戏开始
while(1)
{
if (_kbhit())
{
int ch=_getch();
if (ch==13)
{
flag=!flag;
if (!flag)
{
wColors[0]=FOREGROUND_RED|FOREGROUND_INTENSITY;
rolldice(handle,i+1,row,col,wColors);
//记录游戏者和点数
char buf[20];
itoa(count,buf,10);
textout(handle,1,13+2*count,wMenuColors,1,buf);
textout(handle,3,13+2*count,wMenuColors,1,"点数:");
sum+=i+1;
itoa(sum,buf,10);
textout(handle,9,13+2*count,wMenuColors,1,buf);
}
}
else if (ch==32)//更换游戏者
{
sum=0;
count++;
}
else if (ch=='q' || ch=='Q')
break;
}
if (flag)//随机投筛子
{
i=rand() % 6;
wColors[0]=FOREGROUND_RED|FOREGROUND_INTENSITY;
rolldice(handle,i+1,row,col,wColors);
Sleep(100);
wColors[0]=0;
rolldice(handle,i+1,row,col,wColors);
}
}
}
void rolldice(HANDLE hOutput,int n,int col ,int row,WORD wColors[])
{
switch(n){
case 1: textout(hOutput,row+1,col+1,wColors,1,"●");
break;
case 2: textout(hOutput,row+1,col, wColors,1,"●");
textout(hOutput,row+1,col+2,wColors,1,"●");
break;
case 3: textout(hOutput,row, col+2,wColors,1,"●");
textout(hOutput,row+1,col+1,wColors,1,"●");
textout(hOutput,row+2,col, wColors,1,"●");
break;
case 4: textout(hOutput,row, col, wColors,1,"●");
textout(hOutput,row, col+2,wColors,1,"●");
textout(hOutput,row+2,col, wColors,1,"●");
textout(hOutput,row+2,col+2,wColors,1,"●");
break;;
case 5: textout(hOutput,row, col, wColors,1,"●");
textout(hOutput,row, col+2,wColors,1,"●");
textout(hOutput,row+1,col+1,wColors,1,"●");
textout(hOutput,row+2,col, wColors,1,"●");
textout(hOutput,row+2,col+2,wColors,1,"●");
break;
case 6: textout(hOutput,row, col, wColors,1,"●");
textout(hOutput,row, col+1,wColors,1,"●");
textout(hOutput,row, col+2,wColors,1,"●");
textout(hOutput,row+2,col, wColors,1,"●");
textout(hOutput,row+2,col+1,wColors,1,"●");
textout(hOutput,row+2,col+2,wColors,1,"●");
break;
default:cout<<"投掷骰子失败!"<<endl;
}
}
...全文
819 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq515383106 2010-07-24
  • 打赏
  • 举报
回复
(“_T”的比较好用些),我也遇到同样的问题
cwchenwei2000 2009-09-01
  • 打赏
  • 举报
回复
谢谢!
HuWenjin 2009-08-31
  • 打赏
  • 举报
回复

参见MFC,TCHAR.h中

非UNICODE时
#define __T(x) x

UNICODE时
#define __T(x) L ## x


即_T 不会变成 LPCTSTR,只是在UNICODE将串变成 L"...."表的UNICODE码。
而是自动转换为UNICODE模式



stjay 2009-08-31
  • 打赏
  • 举报
回复
良好习惯,
定义:
BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPCTSTR lpszString);

调用:
textout(hOutput,row+1,col+1,wColors,1,_T("●"));
字符串前加 _T
HuWenjin 2009-08-31
  • 打赏
  • 举报
回复
const char [17] 相当于 LPCTSTR

LPTSTR 相当于 char[17]

你看 const char[17] 当然不能直接转成 char[17]

但是 char[17] 可以强制转成 const 类型的

即,你在提示出错的参数前加一个 (LP C TSTR )... 应该OK了

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧