c ++难题 [英] c++ puzzle

查看:47
本文介绍了c ++难题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码有疑问。什么错误行尝试

来申报?如果下一行能成功,为什么第一行失败

?非常感谢。


A级{

公开:

A(char const *){}

};


int main()

{

char const * a =" hello";

A(a); //错误

A((char const *)a); // ok

返回0;

}

解决方案



Nan Li写道:


我对以下代码有疑问。什么错误行尝试

来申报?如果下一行能成功,为什么第一行失败

?非常感谢。


A级{

公开:

A(char const *){}

};


int main()

{

char const * a =" hello";

A(a); //错误

A((char const *)a); // ok

返回0;

}



这里,


>> A(char const *)



i尝试给它命名,就像''a''


A(char const * a){}


然后在main中,我只是:


char const * p;

A(p);

它完美运行




MC felon写道:


Nan Li写道:


我对以下代码有疑问。什么错误行尝试

来申报?如果下一行能成功,为什么第一行失败

?非常感谢。


A级{

公开:

A(char const *){}

};


int main()

{

char const * a =" hello";

A(a); //错误

A((char const *)a); // ok

返回0;

}



这里,


>> A(char const *)



i尝试给它命名,就像''a''


A(char const * a){}


然后在main中,我只是:


char const * p;

A(p);

完美无缺



您使用了什么编译器?我用g ++。随着你的改变,我仍然得到了相同的旧结果

。我不认为在

签名中添加一个正式参数会改变一些东西。


这里的错误行A(a)实际上是与A a相同。我还是尝试了'A(a)'','A(((a))''',......它们都是一样的。


我不知道你在说什么。在所有c ++编译器中普遍接受的代码是'b
。它是一个简单的OOP代码

,它不使用库。无论你使用什么编译器,它都将是

相同。准确输入此代码:

class a

{

public:

a(char const * a){}

};


void main()

{

char const * pic;

a(图片);

}


如果它不起作用,我会染发粉红色送你一张照片。


I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
A( (char const *)a ); //ok
return 0;
}

解决方案


Nan Li wrote:

I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
A( (char const *)a ); //ok
return 0;
}

here,

>>A( char const * )

i tried giving it a name, like ''a''

A(char const* a) {}

and then in main, i just :

char const* p;
A(p);
it works perfectly



MC felon wrote:

Nan Li wrote:

I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
A( (char const *)a ); //ok
return 0;
}


here,

>>A( char const * )

i tried giving it a name, like ''a''

A(char const* a) {}

and then in main, i just :

char const* p;
A(p);
it works perfectly

What compiler did you use? I use g++. With your change, I still got the
same old results. I don''t think adding a formal parameter in the
signature will change things.

It seems the error line A(a) here is actually the same as ''A a''. I
also tried ''A (a)'', ''A(((a)))'', ... they are all the same thing.


i dont know what you''re talking about. the code is something that''s
universally accepted in all compilers of c++.it''s a plain code of OOP
that uses no libraries. no matter what compiler you use, it''ll be the
same. enter exactly this code:
class a
{
public:
a(char const* a) {}
};

void main()
{
char const* pic;
a(pic);
}

if it doesn''t work, i''ll dye my hair pink and send you a pic of it.


这篇关于c ++难题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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