typedef在const char中失败* [英] typedef is failing in const char*
问题描述
我正在尝试编译以下程序,
#include< iostream>
使用命名空间std;
typedef char * CHAR;
typedef const CHAR CCHAR;
void test(CCHAR pp)
{
cout<<" hello"<< endl;
}
void main()
{
const char * myChar =" tt" ;;
test(myChar);
}
输出:
错误:''test'' :无法将参数1从''const char *''转换为''char
* const''
转换失去限定符
为什么?
怎么解决这个问题?
问候,
Abhishek
Hi,
I am trying to compile the following program,
#include <iostream>
using namespace std;
typedef char* CHAR;
typedef const CHAR CCHAR;
void test(CCHAR pp)
{
cout<<"hello"<<endl;
}
void main()
{
const char* myChar = "tt";
test(myChar);
}
Output:
error: ''test'' : cannot convert parameter 1 from ''const char *'' to ''char
*const ''
Conversion loses qualifiers
Why ?
How to resolve this ?
Regards,
Abhishek
推荐答案
dwaach写道:
dwaach wrote:
我正在尝试编译以下计划,
#inc lude< iostream>
使用命名空间std;
typedef char * CHAR;
Hi,
I am trying to compile the following program,
#include <iostream>
using namespace std;
typedef char* CHAR;
我建议:
typedef char * char_ptr;
I would suggest:
typedef char* char_ptr;
typedef const CHAR CCHAR;
typedef const CHAR CCHAR;
好的,现在CCHAR是一个常量CHAR,即一个指向可变的
字符的常量指针。这是你想要的,还是你想要CCHAR是一个可变的
指向const char的指针?
Ok, now CCHAR is a const CHAR, i.e., a constant pointer to a mutable
character. Is that what you want, or do you want CCHAR to be a mutable
pointer to a const char?
>
无效测试(CCHAR pp)
{
cout<<" hello"<< endl;
>
void test(CCHAR pp)
{
cout<<"hello"<<endl;
为什么你好?你的意思是:
cout<< pp<< ''\ n'';
Why "hello"? Do you mean:
cout << pp << ''\n'';
}
void main()
}
void main()
int main()
int main ()
>
{
const char * myChar =" tt" ;;
>
{
const char* myChar = "tt";
现在,这是一个指向const char的可变指针,而不是指向
char的const指针。
Now, this is a mutable pointer to a const char and not a const pointer to a
char.
test(myChar);
test(myChar);
这应该无法编译,因为test()需要不同的参数。
This should fail to compile because test() requires a different argument.
}
输出:
错误:''test'':无法将参数1从''const char *''转换为''char
* const''
转换失去限定符
}
Output:
error: ''test'' : cannot convert parameter 1 from ''const char *'' to ''char
*const ''
Conversion loses qualifiers
正如所料!
As expected!
>
为什么?
>
Why ?
见上文。
See above.
如何解决这个问题?
How to resolve this ?
typedef char const * CCHAR;
Best
Kai-Uwe Bux >
dwaach写道:
dwaach wrote:
我正在尝试编译以下程序,
#include< iostream>
使用命名空间std;
typedef char * CHAR;
typedef const CHAR CCHAR;
Hi,
I am trying to compile the following program,
#include <iostream>
using namespace std;
typedef char* CHAR;
typedef const CHAR CCHAR;
为什么要这么麻烦?
这个创建一个类型''const指针指向char'',而不是''指向const char''。
Why bother?
This creates a type ''const pointer to char'', not ''pointer to const char''.
void test(CCHAR pp)
{
cout<<" hello"<< endl;
}
void main()
void test(CCHAR pp)
{
cout<<"hello"<<endl;
}
void main()
main返回int。
main returns int.
{
const char * myChar =" tt";
test(myChar);
}
为什么?
如何解决这个问题?
{
const char* myChar = "tt";
test(myChar);
}
Why ?
How to resolve this ?
消除愚蠢的typedef。
-
Ian Collins。
Do away with the silly typedefs.
--
Ian Collins.
在文章< 11 ********************* @ i42g2000cwa.googlegroups。 com>,
" dwaach" < xb ************** @ gmail.comwrote:
In article <11*********************@i42g2000cwa.googlegroups. com>,
"dwaach" <xb**************@gmail.comwrote:
我正在尝试编译以下程序,
#include< iostream>
使用命名空间std;
typedef char * CHAR;
typedef const CHAR CCHAR;
void test(CCHAR pp)
{
cout<<" hello"<< endl;
}
无效main()
{
const char * myChar =" tt";
test(myChar);
}
输出:
错误:''test'':无法从''const char *转换参数1 ''to''char
* const''
转换失去资格赛
为什么?
如何解决这个问题?
Hi,
I am trying to compile the following program,
#include <iostream>
using namespace std;
typedef char* CHAR;
typedef const CHAR CCHAR;
void test(CCHAR pp)
{
cout<<"hello"<<endl;
}
void main()
{
const char* myChar = "tt";
test(myChar);
}
Output:
error: ''test'' : cannot convert parameter 1 from ''const char *'' to ''char
*const ''
Conversion loses qualifiers
Why ?
How to resolve this ?
欢迎来到typedef的疯狂世界。你正在使CHAR const
(即char *)不是CHAR指向的值。你修复它:
typedef const char * CCHAR;
Welcome to the crazy world of typedefs. You are making the CHAR const
(ie the char*) not the value that CHAR points to. You fix it by:
typedef const char* CCHAR;
这篇关于typedef在const char中失败*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!