Simple Tree Syntax Error

T

Travis

I'm creating a real simple tree. No sorting and every node can have
infinite children.

// TreeNode.h

#ifndef TREENODE_H
#define TREENODE_H

#include <iostream>
#include <iomanip>
using namespace std;

// forward declare the tree class
template< class NODE_TYPE > class Tree;

// template definition of tree node
template< class NODE_TYPE >
class TreeNode
{
// be friends with the tree class
friend class Tree< NODE_TYPE >;

private:
typedef TreeNode<NODE_TYPE> Node;
// can have infinite children
std::vector<int> children;

NODE_TYPE data;

public:
// constructor
TreeNode( const NODE_TYPE &d ) : data(d)
{
// allocate memory for children nodes
//children = new std::vector<TreeNode< NODE_TYPE > *>;
}

// accessor
NODE_TYPE getData() const
{
return data;
}
};

#endif

That is the declaration for my tree node. G++ keeps saying
std::vector<int> children; has a syntax error. Sure enough if I
comment the line out, everything compiles. I'm going nuts though
trying to figoure out the error, I dont see it.

Help!
 
D

Default User

Travis said:
I'm creating a real simple tree. No sorting and every node can have
infinite children.

// TreeNode.h

#ifndef TREENODE_H
#define TREENODE_H

#include <iostream>
#include <iomanip>
That is the declaration for my tree node. G++ keeps saying
std::vector<int> children; has a syntax error. Sure enough if I
comment the line out, everything compiles. I'm going nuts though
trying to figoure out the error, I dont see it.

#include <vector>



Brian
 
K

Kai-Uwe Bux

Travis said:
I'm creating a real simple tree. No sorting and every node can have
infinite children.

// TreeNode.h

#ifndef TREENODE_H
#define TREENODE_H

#include <iostream>
#include <iomanip>
using namespace std;

// forward declare the tree class
template< class NODE_TYPE > class Tree;

// template definition of tree node
template< class NODE_TYPE >
class TreeNode
{
// be friends with the tree class
friend class Tree< NODE_TYPE >;

private:
typedef TreeNode<NODE_TYPE> Node;

Right now, you are within the template TreeNode. Thus, the identifier
TreeNode already is a shorthand for TreeNode<NODE_TYPE>. There is no need
to abbreviate that any further.

// can have infinite children
std::vector<int> children;

Don't you mean:

std::vector said:
NODE_TYPE data;

public:
// constructor
TreeNode( const NODE_TYPE &d ) : data(d)
{
// allocate memory for children nodes
//children = new std::vector<TreeNode< NODE_TYPE > *>;

Now this is going to be the trick part.
}

// accessor
NODE_TYPE getData() const
{
return data;
}
};

#endif

That is the declaration for my tree node. G++ keeps saying
std::vector<int> children; has a syntax error. Sure enough if I
comment the line out, everything compiles. I'm going nuts though
trying to figoure out the error, I dont see it.

What is a vector of integers supposed to do for you?



Best

Kai-Uwe Bux
 
T

Travis

Right now, you are within the template TreeNode. Thus, the identifier


Don't you mean:

std::vector< TreeNode* > children;





Now this is going to be the trick part.



What is a vector of integers supposed to do for you?

Best

Kai-Uwe Bux

I switched it to INTs to make sure it wasn't a problem with me using
TreeNode< NODE_TYPE > *>. I just forgot to change it back before
pasting it here. Thank you though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top