且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建一个初始值为NULL的动态指针数组

更新时间:2023-12-01 07:58:40

糟糕。我的主题不正确,我想在我的班级目录中创建我的

类目录的动态数组。

Oops. My subject is incorrect, I want to create a dynamic array of my
class Directory, within my class Directory.


sa *** @ murdocks.on.ca 写道:

我需要(好吧,我想)在我的班级目录中创建我班级目录的动态数组,

(你能闻到灾难吗?)


每个目录都可以有子目录,所以我想把它们放在一个

数组中。该应用程序编译但中止但没有给我任何有用的

信息。


我怀疑正在发生的是无限递归。每个目录

对象创建一个子目录数组,每个子目录都有一个

子导演的数组......


我认为它基本上是以递归方式自杀。


有没有办法声明这个数组,以便值为NULL

最初?因此(我认为)消除了递归问题。然后

,因为我添加ACTUAL子目录它应该工作。当我删除

子目录时,我取数组项并将其设置为NULL ...或者我的

理论如下。


目前在我的标题中我声明我的数组如下:


目录*目录; //指向一个目录数组的指针


我在我的.cpp文件中一直在实例化它:


目录=新目录[MaxSubDirs];


有没有办法自动将值设置为NULL?我不能循环

因为它已经死了。我需要按照

的顺序(当然这不起作用)


目录=新目录[MaxSubDirs] = NULL;


我意识到我应该可以用链接的

列表做同样的事情,但我认为使用数组会更快(如果它是

可能)由于作业到期日期。


我提到我是学生吗?我是学生。
I need (okay, I want) to make a dynamic array of my class ''Directory'',
within my class Directory (Can you already smell disaster?)

Each Directory can have subdirectories so I thought to put these in an
array. The application compiles but aborts without giving me any useful
information.

What I suspect is happening is infinite recursion. Each Directory
object creates an array of Subdirectories each of which has an array of
sub-directorires...

I think it basically is recursively killing itself.

Is there some way to declare this array so that the values are NULL
initially? Thus (I think) doing away with the recursion problem. Then
as I add ACTUAL sub-directories it should work. As I delete
Sub-directories I take the array item and set it to NULL... or so my
theory goes.

currently in my header I declare my array like this:

Directory *Directories; // pointer to an array of Directories

and I had been instantiating it like this in my .cpp file:

Directories = new Directory[MaxSubDirs];

is there some way to automatically set the values to NULL? I can''t loop
because it has already died by then. I need something in the order of
(of course this does not work)

Directories = new Directory[MaxSubDirs] = NULL;

I realize that I should be able to do the same thing with a linked
list, but I think that using the array will be faster (if it is
possible) due to the date the assignment is due.

Did I mention I am a student? I am a student.



使用std :: vector,std :: set或std :: list:


class File;


类目录

{

目录& parent_;

std :: string name_;

std :: set< Filefiles_;

std :: set< Directorysubdirs_;

public:

Directory(const Directory& parent,const std :: string& name)

:parent_(parent)

,name_(姓名)

,files_()

,subdirs_()

{}


void AddDir(const std :: string& name)

{

subdirs_.insert(目录(* this,name));

}


// ...

};


干杯! --M

Use std::vector, std::set or std::list:

class File;

class Directory
{
Directory& parent_;
std::string name_;
std::set<Filefiles_;
std::set<Directorysubdirs_;
public:
Directory( const Directory& parent, const std::string& name )
: parent_( parent )
, name_( name )
, files_()
, subdirs_()
{}

void AddDir( const std::string& name )
{
subdirs_.insert( Directory( *this, name ) );
}

// ...
};

Cheers! --M


sa***@murdocks.on。 ca 写道:

我需要(好吧,我想)制作我的班级目录的动态数组,

我的班级目录(你能闻到灾难吗?)


每个目录都有子目录,所以我想把它们放在一个

数组中。应用程序编译但中止但没有给我任何

有用的信息。


我怀疑发生的是无限递归。每个目录

对象创建一个子目录数组,每个子目录都有一个数组

的副主任......


我认为它基本上是以递归方式自杀。


有没有办法声明这个数组,以便值为NULL

最初?因此(我认为)消除了递归问题。然后

,因为我添加ACTUAL子目录它应该工作。当我删除

子目录时,我取数组项并将其设置为NULL ...或者我的

理论如下。


目前在我的标题中我声明我的数组如下:


目录*目录; //指向一个目录数组的指针


我在我的.cpp文件中一直在实例化它:


目录=新目录[MaxSubDirs];


有没有办法自动将值设置为NULL? [..]
I need (okay, I want) to make a dynamic array of my class ''Directory'',
within my class Directory (Can you already smell disaster?)

Each Directory can have subdirectories so I thought to put these in an
array. The application compiles but aborts without giving me any
useful information.

What I suspect is happening is infinite recursion. Each Directory
object creates an array of Subdirectories each of which has an array
of sub-directorires...

I think it basically is recursively killing itself.

Is there some way to declare this array so that the values are NULL
initially? Thus (I think) doing away with the recursion problem. Then
as I add ACTUAL sub-directories it should work. As I delete
Sub-directories I take the array item and set it to NULL... or so my
theory goes.

currently in my header I declare my array like this:

Directory *Directories; // pointer to an array of Directories

and I had been instantiating it like this in my .cpp file:

Directories = new Directory[MaxSubDirs];

is there some way to automatically set the values to NULL? [..]



在初始化列表中:


...目录(新目录[MaxSubDirs]()) ...


并考虑使用std :: vector,也可以初始化为
。想象一下''目录''被宣布为


std :: vector<目录*目录;


然后你做


...目录(MaxSbDirs,(目录*)NULL)...


(虽然你不是真的需要它,矢量可以增长需要

)。


V

-

请删除大写''A'通过电子邮件回复

我没有回复最热门的回复,请不要问

In the initialiser list:

... Directories(new Directory[MaxSubDirs]()) ...

And consider using std::vector instead, which can also be
initialised. Imagine that ''Directories'' is declared as

std::vector<Directory*Directories;

then you do

... Directories(MaxSbDirs, (Directory*)NULL) ...

(although you don''t really need that, vector can grow as
needed).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask