cannot convert from 'const char *' to 'LPCTSTR'

Hello i am using visual studio 2013 on a windows 7 64 bit. I am trying to build a huffman tree, i have actually built it but i have problems when i try to use cyrllic i manage to get to this situation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <tchar.h>
#include <cstring>
#include <iostream>
#include <locale>
#include <windows.h>
#include <cstdlib>

using namespace std;

typedef unsigned char byte;
byte text[] = "текст на кирилица";



int text_len;

struct Huf {
	byte id;
	int wh;
	Huf *left, *right;
};

struct List {
	List *next;
	Huf *tree;
};

List *head;
char code[256];

void createList();
void writeList();
void delList(List *);
void addList(Huf *);
Huf *findDels();
void createTree();
void rlrootTree(Huf *, unsigned);

int main()
{
	text_len = strlen((const char*)text);
	createList();
	writeList();
	createTree();
	cout << "writeCodes\n";
	rlrootTree(head->tree, 0);
	cout << endl;
	system("pause");
	return 0;
}

void createList()
{
	int i;
	int ch[256] = { 0 };
	for (i = 0; i<text_len; i++) ch[text[i]]++;
	List *l;
	Huf *h;
	head = 0;
	for (i = 0; i<255; i++) if (ch[i]>0)
	{
		h = new Huf;
		h->id = i; h->wh = ch[i];
		h->left = 0; h->right = 0;
		l = new List;
		l->tree = h;
		l->next = head; head = l;
	}
}

void writeList()
{
	cout << "writeList\n";
	List *l = head;
	while (l)
	{
		
		cout << (l->tree)->id << " ";
		l = l->next;
	}
	cout << endl;
	l = head;
	while (l)
	{
		cout << (l->tree)->wh << " ";
		l = l->next;
	}
	cout << endl;
}

void delList(List *l)
{
	List *lp, *lc;
	if (l == head) { head = l->next; delete l; }
	else
	{
		lp = head; lc = lp->next;
		while (lc != l) { lp = lc; lc = lc->next; }
		lp->next = lc->next; delete lc;
	}
}

void addList(Huf *h)
{
	List *l = new List;
	l->tree = h;
	l->next = head;
	head = l;
}

Huf *findDels()
{
	List *l = head, *sm = head;
	Huf *h;
	while (l)
	{
		if ((l->tree)->wh < (sm->tree)->wh) sm = l;
		l = l->next;
	}
	h = sm->tree;
	delList(sm);
	return h;
}

void createTree()
{
	Huf *h, *h1, *h2;
	while (head->next)
	{
		h1 = findDels();
		h2 = findDels();
		h = new Huf;
		h->id = ' '; h->wh = h1->wh + h2->wh;
		h->left = h1; h->right = h2;
		addList(h);
	}
}

void rlrootTree(Huf *h, unsigned index)
{
	if (h)
	{
		code[index] = '0';
		rlrootTree(h->right, index + 1);
		if (h->left == 0)
		{
			code[index] = '\0';
			cout << h->id << "->" << code << " ";
		}
		code[index] = '1';
		rlrootTree(h->left, index + 1);
	}
}


All that is left to do is use AnsiToOem() but i get the cannot convert from 'const char *' to 'LPCTSTR', i know that LPCTSTR is a windows 4 byte, so my question is there a way ?
Last edited on
1. Please go back and edit you post to be nicely tagged and formatted.

"How to use code tags"
http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
8
9
10
// little C++ Shell cog wheel button ---->
// (if missing try refreshing your browser)
#include <iostream>
using namespace std;

int main() {
    cout << "Hello code tags!\n";

    return 0;
}


2. LPCTSTR is actually a typedef of either const wchar_t* (if you're project in configured to build for the Unicode Character Set) or const char* (if configured for the Multi-Byte Character Set or default.)

Likewise, AnsiToOem() is actually a pre-processor macro than ends up evaluating to either AnsiToOemA() (for ANSI builds) or AnsiToOemW() (for Unicode builds.)

Your problem will be solved if you used AnsiToOemA() rather than AnsiToOem().

Andy
Last edited on
It doesnt work with either AnsiToOemA() and AnsiToOem(), i tried char32_t, same ..
What code doesn't work with either AnsiToOemA() and AnsiToOem()? (I can't see it in use in you code above.)

Actually, I was being dozy. There is no AnsiToOemA -- AnsiToOem maps to CharToOemA or CharToOemW.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <windows.h>
#include <iostream>
using namespace std;

int main() {
    UINT oldCP = GetConsoleOutputCP();
    SetConsoleOutputCP(437);

    const char msg[] = "Déjà-vu, oui ? n'est pas ?";

    cout << "Original message:\n";
    cout << msg << "\n";
    cout << "\n";

    cout << "After CharToOemA ()\n";
    char oem_buffer[64] = "";
    CharToOemA(msg, oem_buffer);
    cout << oem_buffer << "\n";
    cout << "\n";

    cout << "After OemToCharA()\n";
    char ansi_buffer[64] = "";
    OemToCharA(oem_buffer, ansi_buffer);
    cout << ansi_buffer << "\n";
    cout << "\n";

    SetConsoleOutputCP(oldCP);

    return 0;
}


Original message:
DÚjÓ-vu, oui ? n'est pas ?

After CharToOemA ()
Déjà-vu, oui ? n'est pas ?

After OemToCharA()
DÚjÓ-vu, oui ? n'est pas ?


What does not working mean? (i.e. what error message)

Andy
So this is one thread from here: http://www.cplusplus.com/forum/windows/9797/ , been very helpful but:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <locale>
#include <windows.h>

using namespace std;

int main() {
	char example[] = "Текст на кирилица";
	AnsiToOem(example, example);
	cout << example << endl;
	system("pause");
	return 0;
}


when i try to use it my main function from the code I posted
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	text_len = strlen((const char*)text);
	createList();
	writeList();
	createTree();
	AnsiToOem(text, text);
	cout << "writeCodes\n";
	rlrootTree(head->tree, 0);
	cout << endl;
	system("pause");
	return 0;
}


Error 1 error C2664: 'BOOL CharToOemA(LPCSTR,LPSTR)' : cannot convert argument 1 from 'byte [18]' to 'LPCSTR' c:\users\user\documents\visual studio 2013\projects\test11\test11\source.cpp 43 1 test11
2 IntelliSense: argument of type "byte *" is incompatible with parameter of type "LPCSTR" c:\Users\user\Documents\Visual Studio 2013\Projects\test11\test11\Source.cpp 43 12 test11
3 IntelliSense: argument of type "byte *" is incompatible with parameter of type "LPSTR" c:\Users\user\Documents\Visual Studio 2013\Projects\test11\test11\Source.cpp 43 18 test11
Last edited on
As I can see from your earlier post, text is a buffer of unsigned chars. This cannot be passed to a function expecting a (signed) char* without casting, as you are already doing when calling strlen().

You could do the same thing when calling AnsiToOem (without the const), but do you actually need text to be unsigned chars? The "helpful" example you post is using char rather than unsigned char.

Andy

PS Working with Unicode or even UTF-8 strings might be better than using OemToChar (as OemToAnsi is now called.)
Last edited on
Yes i am heading to that direction using Unicode or UTF-8, ANSI is just making things more complex, i just have to do some recoding and they i will post the result hope it gets something out..
Last edited on
Topic archived. No new replies allowed.