构造函数&默认参数 [英] Constructor & default arguments

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

问题描述

此代码不会编译:


主要功能:


点临时=新点(*(新点(5, 5)));


在Dot类中:


//具有默认参数的构造函数。

点: :Dot(Point& point = *(new Point(5,5)),

Color& color = *(new Color(100,125,106)))

{

}


它抱怨:


Main.cpp

[警告]在函数`int main(int,char **)'':

Main.cpp

没有匹配函数来调用`Dot :: Dot(Point& ;)''

Dot.h

候选人是:Dot :: Dot(Dot&)

Dot.h

Dot :: Dot(Point&,Color&)

Main.o(。text + 0x84)

[警告]在函数`main'':

[链接器错误]未定义引用`Dot :: Dot()''


我正在使用DEV-C ++和gcc。


为什么找不到匹配的构造函数:


Dot :: Dot(P oint&,Color&)


- 指令

This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}

It complains that:

Main.cpp
[Warning] In function `int main(int, char**)'':
Main.cpp
no matching function for call to `Dot::Dot(Point&)''
Dot.h
candidates are: Dot::Dot(Dot&)
Dot.h
Dot::Dot(Point&, Color&)
Main.o(.text+0x84)
[Warning] In function `main'':
[Linker error] undefined reference to `Dot::Dot()''

I''m using DEV-C++ and gcc.

Why doesn''t it find the matching constructor:

Dot::Dot(Point&, Color&)

--The Directive

推荐答案



指令 <第*********** @ hotmail.com>在消息新闻中写道:84 ************************** @ posting.google.c om ...

"The Directive" <th***********@hotmail.com> wrote in message news:84**************************@posting.google.c om...
这段代码不会编译:

主要功能:

点临时=新点(*(新点(5,5)));

//带默认参数的构造函数。
Dot :: Dot(Point& point = *(new Point(5,5)),
Color& ; color = *(新颜色(100,125,106)))
{
}
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}




我无法确定使用你提供的代码片段,但我怀疑在那个点

中,新点(......)执行时你尚未提供默认参数。

必须在使用它的地方之前看到带有默认参数的声明。

例如:


void func(int );


func();


void func(int x = 3){}


不会编译,因为在调用func()时,它没有看到默认值支持

参数。



I can''t tell for sure with the snippet you''ve provided, but I suspect that at the point
where the "new Dot(..." is executed you haven''t provided the default arguments yet.
The declaration with the defaulted arguments must be seen before the place it is used.
For example:

void func(int);

func();

void func(int x = 3) { }

won''t compile because at the point func() is called, it hasn''t seen the default values for
the arguments.




" The Directive" <第*********** @ hotmail.com>在消息中写道

news:84 ************************** @ posting.google.c om ...

"The Directive" <th***********@hotmail.com> wrote in message
news:84**************************@posting.google.c om...
此代码不会编译:

主要功能:

点临时=新点(*(新点(5,5)) );


temp的类型为Dot,但你试图指定一个Dot *(这是

" new Dot"返回)相反,调用构造函数的正确方法可能是:


点温度(*新点(5,5)) ;


涉及* new的代码通常会导致内存泄漏!你将需要

忘记许多Java习惯。你有一本不错的C ++书吗?

在Dot课程中:

//默认参数的构造函数。
Dot :: Dot(Point& point = *(新点(5,5)),
颜色和颜色= *(新颜色(100,125,106)))



首先,您只能在函数

声明中定义默认参数。 (在你的班级内部为构造函数)。


此外,你再次使用* new。在C ++中,你永远不会使用new没有后来

使用删除,因为C ++没有垃圾收集。既然你没有
实际存储指针,你就不能删除它们,并且每次使用默认参数时都会得到

内存泄漏。

它抱怨:

Main.cpp
[警告]在函数`int main(int,char **)'':
Main.cpp
没有匹配函数调用`Dot :: Dot(Point&)''
Dot.h
候选者是:Dot :: Dot(Dot&)
Dot.h
Dot :: Dot(Point&,Color&)
Main.o(。text + 0x84)
[警告]在函数`main''中:
[链接器错误]未定义引用`Dot :: Dot()''

我正在使用DEV-C ++和gcc。

为什么它找不到匹配构造函数:

Dot :: Dot(Point&,Color&)


如果没有更完整的代码,很难分辨。

- 指令
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );
temp is of type "Dot", but you are trying to assign a Dot* (which is what
"new Dot" returns) to it.

Instead, the correct way to call a constructor might be:

Dot temp (*new Point (5, 5));

Code involving "*new" usually leads to memory leaks! You will need to
forget many Java habits. Do you have a decent C++ book?

In Dot class:

// Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}
First of all, you can only define default arguments in the function
declaration. (inside your class for a constructor).

Also, you''re using *new again. In C++ you never use "new" without later
using "delete", since C++ does not have garbage collection. Since you don''t
actually store the pointers here, you can''t delete them, and you will get a
memory leak any time you use the default arguments.

It complains that:

Main.cpp
[Warning] In function `int main(int, char**)'':
Main.cpp
no matching function for call to `Dot::Dot(Point&)''
Dot.h
candidates are: Dot::Dot(Dot&)
Dot.h
Dot::Dot(Point&, Color&)
Main.o(.text+0x84)
[Warning] In function `main'':
[Linker error] undefined reference to `Dot::Dot()''

I''m using DEV-C++ and gcc.

Why doesn''t it find the matching constructor:

Dot::Dot(Point&, Color&)
It''s hard to tell without more complete code.

--The Directive




HTH

-

KCS



HTH
--
KCS




" The Directive" <第*********** @ hotmail.com> skrev i en meddelelse

news:84 ************************** @ posting.google.c om .. 。

"The Directive" <th***********@hotmail.com> skrev i en meddelelse
news:84**************************@posting.google.c om...
这段代码不会编译:

主要功能:

点临时=新点(*(新点(5,5)) );;;

在Dot类中:

//带默认参数的构造函数。
Dot :: Dot(Point& point = *(new Point(5, 5)),
颜色和颜色= *(新颜色(100,125,106)))
{
}

它抱怨:

Main.cpp
[警告]在函数`int main(int,char **)'':
Main.cpp
没有用于调用`Dot的匹配函数: :Dot(Point&)''
Dot.h
候选人是:Dot :: Dot(Dot&)
Dot.h
Dot :: Dot(Point&,Color& ;)
Main.o(。text + 0x84)
[警告]在函数main中:
[链接器错误]未定义引用`Dot :: Dot()''

我正在使用DEV-C ++和gcc。

为什么找不到匹配的构造函数:

Dot :: Dot( POIN t&,Color&)

- 指令
This code will not compiled:

In main function:

Dot temp = new Dot( *(new Point( 5, 5 )) );

In Dot class:

//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5 )),
Color& color = *(new Color( 100, 125, 106 )) )
{
}

It complains that:

Main.cpp
[Warning] In function `int main(int, char**)'':
Main.cpp
no matching function for call to `Dot::Dot(Point&)''
Dot.h
candidates are: Dot::Dot(Dot&)
Dot.h
Dot::Dot(Point&, Color&)
Main.o(.text+0x84)
[Warning] In function `main'':
[Linker error] undefined reference to `Dot::Dot()''

I''m using DEV-C++ and gcc.

Why doesn''t it find the matching constructor:

Dot::Dot(Point&, Color&)

--The Directive




那段代码是如此令人震惊的错误,我必须建议你


a)在编写C ++时忘记Java。

b)了解C ++ - 特别是何时使用new和const正确性。


一个链接: http://www.parashift.com/ C ++ - faq-lite


亲切的问候

Peter



That code is just so shockfull of errors, I must recommend that you

a) forget about Java when coding C++.
b) learn about C++ - in particular when to use new and const correctness.

One link for you: http://www.parashift.com/C++-faq-lite

Kind regards
Peter


这篇关于构造函数&amp;默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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