无法从constuctor调用另一个构造函数 [英] fail to call another constructor from constuctor

查看:106
本文介绍了无法从constuctor调用另一个构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




1 #include< stdio.h>

2

3 A级{

4 private:

5 int x;

6 public:

7 A(int x){

8 this-> x;

9}

10

11 A(int x,char * y){

12 A(x); < ----------------可能

i知道怎么修这条线?

13}

14};

15

16 int main(){

17返回1;

18}


d.cpp:在构造函数`A :: A(int,char *)''中:

d.cpp:12:错误:声明`x''阴影参数

d.cpp:12:错误:没有匹配函数来调用`A :: A()''

d.cpp: 3:错误:候选人是:A :: A(const A&)

d.cpp:11:错误:A :: A(int,char *)

d.cpp:7:错误:A :: A(int)


谢谢
来自Peter的
(cm **** @ hotmail.com)

hi

1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }

d.cpp: In constructor `A::A(int, char*)'':
d.cpp:12: error: declaration of `x'' shadows a parameter
d.cpp:12: error: no matching function for call to `A::A()''
d.cpp:3: error: candidates are: A::A(const A&)
d.cpp:11: error: A::A(int, char*)
d.cpp:7: error: A::A(int)

thanks
from Peter (cm****@hotmail.com)

推荐答案

只需显式调用构造函数A(),如下所示:

A :: A(x); //第12行
cm****@hotmail.com 写道:
just explicitly call constructor A() as follows:
A::A(x); //line 12
cm****@hotmail.com wrote:

hi


1 #include< stdio.h>

2

3 A类{

4私人:

5 int x;

6 public:

7 A(int x){

8 this-> x;

9}

10

11 A(int x,char * y ){

12 A(x); < ----------------可能

i知道怎么修这条线?

13}

14};

15

16 int main(){

17返回1;

18}


d.cpp:在构造函数`A :: A(int,char *)''中:

d.cpp:12:错误:声明`x''阴影参数

d.cpp:12:错误:没有匹配函数来调用`A :: A()''

d.cpp: 3:错误:候选人是:A :: A(const A&)

d.cpp:11:错误:A :: A(int,char *)

d.cpp:7:错误:A :: A(int)


谢谢
来自Peter的
(cm****@hotmail.com)
hi

1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }

d.cpp: In constructor `A::A(int, char*)'':
d.cpp:12: error: declaration of `x'' shadows a parameter
d.cpp:12: error: no matching function for call to `A::A()''
d.cpp:3: error: candidates are: A::A(const A&)
d.cpp:11: error: A::A(int, char*)
d.cpp:7: error: A::A(int)

thanks
from Peter (cm****@hotmail.com)


laikon< la **** @ gmail.comwrote:
laikon <la****@gmail.comwrote:

> just显式调用构造函数A()如下:
A :: A(x); //第12行
>just explicitly call constructor A() as follows:
A::A(x); //line 12



我尝试时无效。例如,以下

使用gcc编译但无法打印出值3。 -

它打印出一个垃圾值。


我确定这里有一个简单的解释,但有待解决

它是什么,我避免从另一个构建函数调用。


史蒂夫


*******

#include< iostream>


struct A {

int state;

A(int x){

州= x;

};

A(){

A :: A(3);

};

};


int main()

{

使用std :: cout;

使用std :: endl;

A z;

cout<< z.state<< endl;

}

That never works when I try it. For example, the following
compiles with gcc but fails to print out a value of "3" --
it prints a garbage value.

I''m sure there''s a simple explanation, but pending figuring out
what it is, I avoid calling one constructor from another.

Steve

*******
#include <iostream>

struct A {
int state;
A(int x) {
state = x;
};
A() {
A::A(3);
};
};

int main()
{
using std::cout;
using std::endl;
A z;
cout << z.state << endl;
}


cm **** @ hotmail.com 写道:

hi


1 #include< stdio.h>

2

3 A级{

4私人:

5 int x;

6公开:

7 A(int x){

8 this-> x;

9}

10

11 A(int x,char * y){

12 A(x); < ----------------可能

i知道怎么修这条线?

13}

14};

15

16 int main(){

17返回1;

18}


d.cpp:在构造函数`A :: A(int,char *)''中:

d.cpp:12:错误:声明`x''阴影参数

d.cpp:12:错误:没有匹配函数来调用`A :: A()''

d.cpp: 3:错误:候选人是:A :: A(const A&)

d.cpp:11:错误:A :: A(int,char *)

d.cpp:7:错误:A :: A(int)


谢谢
来自Peter的
(cm **** @ hotmail.com)
hi

1 #include <stdio.h>
2
3 class A{
4 private:
5 int x;
6 public:
7 A(int x){
8 this->x;
9 }
10
11 A(int x, char *y){
12 A(x); <---------------- may
i know how to fix this line?
13 }
14 };
15
16 int main(){
17 return 1;
18 }

d.cpp: In constructor `A::A(int, char*)'':
d.cpp:12: error: declaration of `x'' shadows a parameter
d.cpp:12: error: no matching function for call to `A::A()''
d.cpp:3: error: candidates are: A::A(const A&)
d.cpp:11: error: A::A(int, char*)
d.cpp:7: error: A::A(int)

thanks
from Peter (cm****@hotmail.com)



请参阅:
http://www.parashift.com/c++-faq-lit....html#faq-10.3


问候,

Sumit。


See:
http://www.parashift.com/c++-faq-lit....html#faq-10.3

Regards,
Sumit.


这篇关于无法从constuctor调用另一个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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