这段代码出了什么问题? [英] What's wrong with this code?

查看:170
本文介绍了这段代码出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我没有C ++知识。我有一个项目的源代码,并添加了这些新行来从ini文件中的Server部分读取IP和PORT值,现在我无法编译它。有人可以帮我弄这个吗?谢谢。



Hello everyone, I have no C++ knowledge. I have the source code of a project and added these new lines to read IP and PORT values from Server section inside a ini file, and now I can't compile it. Can someone help me with this? Thank you.

if (GetFileAttributes(gamepath.c_str()) != INVALID_FILE_ATTRIBUTES)
    {
        char ip[16];
        int port;

        GetPrivateProfileString(L"Server", L"IP", L"127.0.0.1", ip, _countof(ip), gamepath.c_str());
        GetPrivateProfileInt(L"Server", L"PORT", port, gamepath.c_str());
    }







char ip [16];错误:类型char *的参数与LPWSTR类型的参数不兼容




char ip[16]; Error: argument of type "char*" is incompatible with parameter of type "LPWSTR"

推荐答案

您在Unicode中编译,因此您无法使用char。使用unicode字符串或字符如wchar_t。
You compile in Unicode so you cant use char. Use a unicode string or characters like wchar_t.


hi,

错误信息解释它:

函数驱逐你通过LPWSTR类型的参数,但你传给它一个字符。

所以请将ip [16]转换为LPWSTR它应该可以工作。



函数 mbstowcs [ ^ ]可以帮到你。



a粗略的例子:




the error message explains it :
the function expexts you tp pass an argument of type LPWSTR but you are passing it a char.
so please convert the ip[16] to LPWSTR and it should work.

The function mbstowcs[^] can help you out.

a rough example :

char *mychar;
size_t nSize1 = 1 + strlen( mychar );
LPWSTR wUserName = new WCHAR[nSize1];
mbstowcs( wUserName, mychar, nSize1 );
  // use wUserName but after all the usage do remember delete it
delete []wUserName;





看看这个msdn link [ ^ 对于mbstowcs。



希望这会有所帮助!!



i不知道你的整个代码,这只是命中和试用



Check out this msdn link[^] for mbstowcs.

hope this helps !!

i dont know your whole code, this is all just hit and trial

 char ip[16];
 size_t nSize1 = 1 + strlen( ip );
 LPWSTR wIPName = new WCHAR[nSize1];
 mbstowcs( wIPName, ip, nSize1 );
 //now wIPNmae has the string in required fromat
 GetPrivateProfileString(L"Server", L"IP", L"127.0.0.1", wIPName, _countof(ip),gamepath.c_str());
//the _countof() function is also using 'ip', i hope it does not complain
 
 //when you are done with wIPNmae then delete it
delete []wIPName;


这篇关于这段代码出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆