syntax question
Hi all,
quick question. I can''t get an array of a structure to initialise. the line
myTree[0] = tree{1, 1, 1}; doesnt work.
I also need to be able to determine the length of the array of tree by doing something like tree.length. Whilst this is available in the STL (i.e. for a vector), how would it be achieved using a struct.
Thanks in advance!
#include "stdafx.h"
struct tree
{
int key;
int row;
int parent;
};
int _tmain(int argc, _TCHAR* argv[])
{
tree myTree[4];
myTree[0] = tree{1 ,1, 1};
return 0;
}
You can''t do that because the structures were already initalized (with whatever rubbish values are in memory) during the creation of the array.
I think you can only use that syntax when you''re initializing, like so:
tree myTree[4] = {
{ 1, 1, 1, }, { 1, 1, 1, },
{ 1, 1, 1, }, { 1, 1, 1, },
}
And you can''t really determine the size of the tree unless you have sizeof( myTree ), but this is calculated at compile time. I believe you should use your own variable to keep track of the size.
tree myTree[4] = {
{ 1, 1, 1, }, { 1, 1, 1, },
{ 1, 1, 1, }, { 1, 1, 1, },
}
And you can''t really determine the size of the tree unless you have sizeof( myTree ), but this is calculated at compile time. I believe you should use your own variable to keep track of the size.
Start by giving the structure a constructor.
Then, create the array as an array of pointers.
Initialize it''s entries to NULL.
When you create a new entry in the array, you can now call the constuctor to assign the values of the tree.
To calculate the size, either do as psykr said (efficient way) or write a function that counts all non-NULL values in the array (inefficient way).
It''s a bit brute force but it seems to be more or less what you''re after.
Then, create the array as an array of pointers.
Initialize it''s entries to NULL.
When you create a new entry in the array, you can now call the constuctor to assign the values of the tree.
To calculate the size, either do as psykr said (efficient way) or write a function that counts all non-NULL values in the array (inefficient way).
It''s a bit brute force but it seems to be more or less what you''re after.
You are trying to use array initialisation syntax during an assignment. This is impossible. You can only use it when the array is being defined.
There is no alternative. myTree[0] = {1, 1, 1}; and variants on the same theme are illegal C code.
Note that, when an array is so initialised, any uninitialised element is set to zero.
However, as your syntax ( struct tree {}; ... tree myTree[4]; ) indicates you are using C++, here is another option that is open to you : define a constructor.
Of course, this changes the semantics of the tree type, which then becomes a non-POD type. Whether it is what you want or not is yours to decide.
Your question about the length is vague. Are you looking for the length of the struct (use sizeof), the height of the tree (I don't know what kind of tree structure you are using and I see no pointers), or its number of elements (idem).
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 22, 2003 8:40:49 PM]
tree myTree[4] = { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 }, { 4, 4, 4 }, // Trailing comma is allowed };
There is no alternative. myTree[0] = {1, 1, 1}; and variants on the same theme are illegal C code.
Note that, when an array is so initialised, any uninitialised element is set to zero.
tree myTree[4] = { { 1, 1, 1 }, { 2, 2 }, // myTree[1] is { 2, 2, 0 } { 3, 3, 3 }, // myTree[3] is { 0, 0, 0 }};
However, as your syntax ( struct tree {}; ... tree myTree[4]; ) indicates you are using C++, here is another option that is open to you : define a constructor.
struct tree{ int key, row, parent; tree( int k=0, int r=0, int p=0 ) : key(k), row(r), parent(p) {}};tree myTree[4];myTree[0] = tree( 1, 1, 1 );
Of course, this changes the semantics of the tree type, which then becomes a non-POD type. Whether it is what you want or not is yours to decide.
Your question about the length is vague. Are you looking for the length of the struct (use sizeof), the height of the tree (I don't know what kind of tree structure you are using and I see no pointers), or its number of elements (idem).
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 22, 2003 8:40:49 PM]
Thanks so much for the advice!
what do u mean by non-POD?
also, how could i make it so I coudl use a command like
mytree.size
to return the size of the tree. If i was to make the tree dynamically built.
what do u mean by non-POD?
also, how could i make it so I coudl use a command like
mytree.size
to return the size of the tree. If i was to make the tree dynamically built.
In C++ a struct and a class are the same thing, modulo the default permissions. POD (plain old data) types are how traditional C-style structs and other data types binary-compatible with C are named in the C++ language definition documents.
POD and non-POD types are treated differently. For example, POD types are uninitialised, while non-POD types are zeroed (or have their constructors called). And so on and so forth.
To have tree.size() return the size of the tree, you must define it as a member function.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 22, 2003 9:06:08 PM]
POD and non-POD types are treated differently. For example, POD types are uninitialised, while non-POD types are zeroed (or have their constructors called). And so on and so forth.
To have tree.size() return the size of the tree, you must define it as a member function.
struct tree{ int key, row, parent; int size();};int tree::size(){ // Magic happens here.}
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 22, 2003 9:06:08 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement