I have two c++ win32 console application project. Both have exactly identical code.
#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;
}
The silly thing is that one of them is giving me an error where I try to pass argv[1] and argv[2] into string variables. The error message is as follows:
cannot convert from '_TCHAR *' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
Since when doesn't this work and how can one of two identical projects possibly generate an error?
_TCHARis either defined aswchar_torchardepending on whether the project is set to compile for Unicode or not. When you have a project being compiled for Ansi/MBCS,_TCHAR*(iechar*) can be assigned tostd::string. When you have a project being compiled for Unicode instead,_TCHAR*(iewchar_t*) cannot be assigned tostd::string, you have to usestd::wstringinstead, or convert the Unicode data to Ansi/MBCS at runtime.If you really want the same code to compile in both configurations, you will have to use
std::basic_string<_TCHAR>andstd::basic_ifstream<_TCHAR>, wrap literals in_T(), etc. For example:Windows has been a Unicode OS for a LONG LONG time. Using
_TCHARonly makes sense if you are developing for Windows 9x/ME. Otherwise, you really should be using Unicode only and not_TCHARat all: