![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Error: Type Specifier Omitted?
I need help getting the following code to compile. I am writing a set class implemented as a binary search tree of strings, and have started writing a header file, but for some reason the insert(...) method is causing me problems. I have commented out the .cpp file to get this to work but it does not.
#include <string>
#include <cstdlib>
using std::string;
class BSTSet {
public:
BSTSet();
void insert(string s, TreeNode *node = root); // <-- this is the problem
private:
class TreeNode {
public:
TreeNode(string s, TreeNode *l = NULL, TreeNode *r = NULL);
string value;
TreeNode *left, *right;
};
TreeNode *root;
};BSTSet.h:19: type specifier omitted for parameter `TreeNode' BSTSet.h:19: parse error before `*' token |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
I rearranged your code a little. i think i clearer like that, you dont need to include a class inside another.
i saw that you had (.....TreeNode *r = root) in the insert function. this made no sense to me, since the insert function should take a node pointer and insert it as a left child or right child of the root, and so on.... basicly the insert function CHANGES the root.. have fun with the rest.. it would be cooler if you made a template element instead of string though, this way your tree can take anything from integers to even objects.. #include <string>
#include <cstdlib>
using std::string;
class TreeNode {
private:
string value;
TreeNode* left;
TreeNode* right;
public:
TreeNode(string s, TreeNode *l = NULL, TreeNode *r = NULL)
{
value = s;
left = l;
right = r;
}
};
class BSTSet {
private:
TreeNode *root;
public:
BSTSet();
void insert( string s = NULL, TreeNode *node = NULL )
{
root = node; //do this if tree is empty else you change the root would have a new child/sibling...
}
};
void main()
{
} |
|
|
|
|
|
#3 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
Remember that unlike in Java, types (and functions) must be declared before they can be used in C++. Rearranging fixed the problem as the class was then defined at the time when you referenced it. And as hbe02 suggested, once you get your BST working, try it with templates
![]() |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5
![]() |
There are actually two problems in the code. Firstly, before you can pass a pointer to a function, there needs to be (at a minimum) a forward declaration of the type you are passing. Second, default arguments to functions need to be resolvable to an actual value at compile time. So "root" needs to be some_static_object.root or NULL.
The default value for an argument of a non-static member function cannot be a non-static member of the class. There are two alternatives; #include <string>
#include <cstdlib>
class BSTSet {
public:
BSTSet();
class BSTSet::TreeNode; // so we can declare functions that accept a pointer to TreeNode
void insert(const std::string &s, TreeNode *node);
void insert(const std::string &s) {insert(s, root);};
private:
class TreeNode {
public:
TreeNode(const std::string s, TreeNode *l = NULL, TreeNode *r = NULL);
std::string value;
TreeNode *left, *right;
};
TreeNode *root;
};#include <string>
#include <cstdlib>
class BSTSet {
public:
BSTSet();
class BSTSet::TreeNode; // so we can declare functions that accept a pointer to TreeNode
void insert(const std::string &s, TreeNode *node = 0)
{
if (!node) node = root;
// all the code you intended to have.....
};
private:
class TreeNode {
public:
TreeNode(const std::string s, TreeNode *l = NULL, TreeNode *r = NULL);
std::string value;
TreeNode *left, *right;
};
TreeNode *root;
}; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|