Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 31st, 2006, 11:40 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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;
};
I get the following error when I try to compile this:

BSTSet.h:19: type specifier omitted for parameter `TreeNode'
BSTSet.h:19: parse error before `*' token
I have tried adding incomplete class declarations such as "class BSTSet::TreeNode;" and "class TreeNode" but I can't seem to figure out the problem. Please help! Thanks.
titaniumdecoy is offline   Reply With Quote
Old Apr 1st, 2006, 2:35 AM   #2
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
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()
{

}
hbe02 is offline   Reply With Quote
Old Apr 1st, 2006, 3:15 AM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
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
Jimbo is offline   Reply With Quote
Old Apr 1st, 2006, 3:34 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,207
Rep Power: 5 grumpy is on a distinguished road
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;
};
OR;
#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;
};
One other thing that I've fixed as an aside: eventually, as a project gets bigger, class declarations get moved into separate header files. It is A VERY BAD IDEA to place a using directive into a header file at file scope. That usually means that, if you want a class to have a member of type std::string in a class, it is better to avoid a using directive and to employ the full name (std::string) within the class declaration.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC