【发布时间】:2014-05-29 14:31:07
【问题描述】:
我有两个 c++ win32 控制台应用程序项目。两者的代码完全相同。
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
if (argc != 3){
cout << "Program needs 2 arguments";
}
else{
string filePath = argv[1];
string flag = argv[2];
unsigned long int size;
bool najden = false;
//odpri datoteko
ifstream idatoteka(filePath, ios::in | ios::binary | ios::ate);
if (idatoteka){
if (flag != "-c" && flag != "-e"){
cout << "unknown flag";
}
else{
if (flag == "-c"){
//kompresija
}
else{
//dekompresija
}
}
}
else{
cout << "This file doesn't exist\n";
}
}
system("pause");
return 0;
}
愚蠢的是,其中一个给了我一个错误,我尝试将 argv[1] 和 argv[2] 传递给字符串变量。错误信息如下:
cannot convert from '_TCHAR *' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
这从什么时候开始不起作用,两个相同的项目中的一个怎么可能产生错误?
【问题讨论】:
-
你可以找到你的答案here
-
谢谢。问题解决了。但是编译器如何决定何时使用 wstring 以及何时使用 string?代码是相同的。另外,我可以让它默认使用 wstring(不必总是输入 wstring)吗?
-
在了解了string和wstring之后,通过比较Project属性找到了最终的解决方案。解决方案是将字符集设置为“使用多字节字符集”。然后编译器会让你从 _TCHAR* 传递到字符串。
-
@user1956373 请注意,这种技术(使用 _TCHAR、_tmain 等)必须归类为失败的实验。处理
std::wstring的方式(通常)与处理std::string的方式大不相同;您必须为每种情况编写不同的代码。
标签: c++ string winapi argv tchar