在构造函数中初始化priority_queue [英] Initializing priority_queue in constructor

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

问题描述



大家好,

经过多年的C,我以为我最近会转向C ++。我认为

因为我认为在C中,我会对某些事情产生误解。


我创建了一个类foo,其中包含一个私有变量是一个

优先级队列。在类foo的头文件中,我将其声明为:


class foo {

private:

unsigned int count ;

priority_queue< double,std :: vector< double>,更大< double

pqueue;

};


在构造函数中,我被警告我应该在

成员初始化列表中初始化(因为我是新的,我将所有这些警告添加到

我的编译器)。所以,我知道我能做到:


foo :: foo()

:count(0)

{

}


但现在我被卡住了。如何初始化优先级队列?我希望

拥有一个没有任何东西的优先级队列(即空),并且已经在声明中为我完成了

(我认为......) 。我是否只是通过打开警告来解决问题,我应该忽略

这个关于初始化pqueue的问题?显然,这不是一个错误

我可以继续...但我想知道我该怎么做...


谢谢!


Ray


Hi all,

After many years of C, I thought I''d move to C++ recently. I think
because I think in C, I''m coming to a misunderstanding of something.

I''ve created a class foo which contains a private variable which is a
priority queue. In class foo''s header file, I declared it as:

class foo {
private:
unsigned int count;
priority_queue<double, std::vector<double>, greater<double
pqueue;
};

In the constructor, I''m being warned that I should initialize in the
member initialization list (as I am new, I added all these warnings to
my compiler). So, I know I could do:

foo::foo ()
: count (0)
{
}

but now I''m stuck. How does one initialize a priority queue? I want
to have a priority queue with nothing (i.e., empty) and it''s already
done that for me in the declaration (I think...). Did I just get
myself in trouble by turning on the warnings and I should just ignore
this one about initializing pqueue? Obviously, this isn''t an error
and I can continue...but I would like to know what should I do...

Thanks!

Ray

推荐答案



你好Alf,


感谢您的回复!对不起,但我忘了澄清它确实

编译得很好......但是由于警告标志我正在使用(-Weffc ++

in g ++),I '得到警告(而不是编译错误)。因为我是新的b $ b,我想知道是否要忽略这个警告(即,

什么是最佳做法)。根据我的手册页,-Weffc ++确实如此
这个:


-Weffc ++(仅限C ++)

警告违反以下风格指南

来自Scott

Meyers的'有效C ++书:


*项目11:定义一个复制构造函数和一个分配

运算符

用于具有动态分配内存的类。


*项目12:首选初始化为

施工人员。


....


我把剩下的休息了,因为它似乎很明显是警告我因为

项目12.警告(我用你发布的代码获得

以下......谢谢你把它放在一起!)是:


foo.c:在构造函数''foo :: foo()''中:

foo.c:16:警告:''foo :: pqueue''应该被初始化在会员中

初始化列表


所以,我想我想知道的是一个priority_queue(矢量等)

被初始化为了满足这个编译器警告或者我应该只是忽略它吗?


谢谢!


Ray
4月26日晚9:18,Alf P. Steinbach < al ... @ start.nowrote:

Hi Alf,

Thank you for your reply! Sorry, but I forgot to clarify that it does
compile fine... But because of the warning flag I''m using (-Weffc++
in g++), I''m getting a warning (and not a compiling error). As I''m
new, I''d like to know whether or not to ignore this warning (i.e.,
what is "best" practice). According to my man pages, -Weffc++ does
this:

-Weffc++ (C++ only)
Warn about violations of the following style guidelines
from Scott
Meyers'' Effective C++ book:

* Item 11: Define a copy constructor and an assignment
operator
for classes with dynamically allocated memory.

* Item 12: Prefer initialization to assignment in
constructors.

....

I cut the rest out as it seems obvious it is warning me because of
Item 12. The warning (which I do get with the code you posted
below...thank you for putting it together!) is:

foo.c: In constructor ''foo::foo()'':
foo.c:16: warning: ''foo::pqueue'' should be initialized in the member
initialization list

So, I guess what I am wondering is can a priority_queue (vector, etc.)
be "initialized" to satisfy this compiler warning or should I just
ignore it?

Thank you!

Ray


On Apr 26, 9:18 pm, "Alf P. Steinbach" <al...@start.nowrote:

发布最小的完整示例是个好主意。


因为它是不可能说出你做错了什么。


以下编译好:


< ;代码>

#include< iostream>

#include< queue>

#include< vector>


使用命名空间std;


class foo {

private:

unsigned int count;

priority_queue< double,std :: vector< double>,more< double pqueue;

public:

foo();
< br $>
};


foo :: foo()

:count(0)

{< br $>

}


int main()

{

foo o;}


< / code>
It''s a good idea to post a minimal complete example.

As it is it''s impossible to say what you''re doing wrong.

The following compiles fine:

<code>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

class foo {
private:
unsigned int count;
priority_queue<double, std::vector<double>, greater<double pqueue;
public:
foo();

};

foo::foo ()
: count (0)
{

}

int main()
{
foo o;}

</code>




很抱歉,但忘记添加我使用的是g ++ 4.1.2版(不确定

如果这很重要的话)...


Ray


4月26日晚上9:50,Ray< rayky .. 。@ gmail.comwrote:

Sorry, but forgot to add that I am using g++ version 4.1.2 (not sure
if this matters)...

Ray

On Apr 26, 9:50 pm, Ray <rayky...@gmail.comwrote:

编译好......但由于我正在使用的警告标志(-Weffc ++

in g ++),我收到警告(而不是编译错误)。因为我是新的b $ b,我想知道是否要忽略这个警告(即,

什么是最佳做法)。根据我的手册页,-Weffc ++确实

这个:
compile fine... But because of the warning flag I''m using (-Weffc++
in g++), I''m getting a warning (and not a compiling error). As I''m
new, I''d like to know whether or not to ignore this warning (i.e.,
what is "best" practice). According to my man pages, -Weffc++ does
this:




Ray写道:

[snip]

Ray wrote:
[snip]

我创建了一个包含私有变量的类foo,这是一个

优先队列。在类foo的头文件中,我将其声明为:


class foo {

private:

unsigned int count ;

priority_queue< double,std :: vector< double>,更大< double

pqueue;

};


在构造函数中,我被警告我应该在

成员初始化列表中初始化(因为我是新的,我将所有这些警告添加到

我的编译器)。所以,我知道我能做到:


foo :: foo()

:count(0)

{

}


但现在我被卡住了。如何初始化优先级队列?我希望

拥有一个没有任何东西的优先级队列(即空),并且已经在声明中为我完成了

(我认为......) 。我刚收到
I''ve created a class foo which contains a private variable which is a
priority queue. In class foo''s header file, I declared it as:

class foo {
private:
unsigned int count;
priority_queue<double, std::vector<double>, greater<double
pqueue;
};

In the constructor, I''m being warned that I should initialize in the
member initialization list (as I am new, I added all these warnings to
my compiler). So, I know I could do:

foo::foo ()
: count (0)
{
}

but now I''m stuck. How does one initialize a priority queue? I want
to have a priority queue with nothing (i.e., empty) and it''s already
done that for me in the declaration (I think...). Did I just get



[snip]


你试过吗:


foo :: foo()

:pqueue(),

count(0)

{


}


警告只是意味着你应该在进入构造函数体之前初始化每个成员

变量。我只是谨慎使用C ++

,所以我希望上面的代码是正确的。我试图用/ b $ b做的代码是在

实际进入构造函数体之前调用priority_queue默认构造函数。


克里斯

[snip]

Have you tried:

foo::foo ()
: pqueue(),
count(0)
{

}

The warning just meant that you should initialized each of the member
variables before entering the constructor body. I only used C++
sparingly, so I hope the code above is correct. What I was trying to
do with the code was to call priority_queue default constructor before
actually entering the constructor body.

Chris


这篇关于在构造函数中初始化priority_queue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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