By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

[Merged] "'WordNode' does not name a type"? Help with Binary Search Tree

P: 9
When I try to compile the below files, the complier says:
the WordNode h in BST.cpp: "WordNode" does not name a type
also in BST.cpp: "WordNode" undeclared, first use of this function

So, any advice? Sorry for the huge post, just wanted to make sure you can understand the problem. As it is, I'm lost.... I've included the WordNode header file, so why can't I create/use WordNode objects/methods (in BST)?

WordNode.h
Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef RANDOM
  3. #define RANDOM
  4. #include <string> 
  5. #include <fstream>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <dinput.h>
  9. #include "BST.h"
  10.  
  11. using namespace std;
  12.  
  13. class WordNode 
  14. public:
  15. WordNode(string name);
  16. string word;
  17. void getLeft( WordNode);
  18. void getRight (WordNode); 
  19. WordNode *left;
  20. WordNode *right;
  21. bool hasLeft;
  22. bool hasRight;
  23. friend ostream& operator<<(ostream& os, const WordNode& wn);
  24.  
  25. }; 
  26.  
  27. #endif
  28.  
  29. WordNode.cpp
  30. #include "WordNode.h"
  31. #include <string> 
  32. #include <fstream>
  33. #include <string> 
  34. #include <fstream>
  35. #include <vector>
  36. #include <iostream>
  37. #include <string>
  38. #include <stack>
  39. #include <queue>
  40. #include <dinput.h>
  41. #include "WordNode.h"
  42.  
  43. using namespace std;
  44.  
  45. WordNode::WordNode( string w)
  46. {
  47. word = w; 
  48. }
  49.  
  50. void WordNode::getLeft( WordNode lptr)
  51. {
  52. hasLeft = true;
  53. left = &lptr;
  54. }
  55.  
  56. void WordNode::getRight( WordNode rptr)
  57. {
  58. hasRight = true;
  59. right = &rptr;
  60. }
  61.  
  62. ostream& operator<<(ostream& os, const WordNode& wn)
  63. {
  64. os << wn.word;
  65. return os;
  66. };
  67.  
  68.  
Then......
BST.h
Expand|Select|Wrap|Line Numbers
  1.  #ifndef RANDOM 
  2. #define RANDOM
  3.  
  4. #include <string> 
  5. #include <fstream>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <dinput.h>
  9. //#include "BST.h"
  10. #include "WordNode.h"
  11.  
  12. using namespace std;
  13.  
  14. class BST
  15. {
  16. public:
  17. BST();
  18. void insert();
  19. void print();
  20. WordNode h;
  21. //WordNode head;
  22. };
  23.  
  24. #endif
  25.  
And finally, BST.cpp

Expand|Select|Wrap|Line Numbers
  1.  #include <string> 
  2. #include <fstream>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <dinput.h>
  6. #include "BST.h"
  7. #include "WordNode.h"
  8.  
  9. using namespace std;
  10.  
  11. BST::BST()
  12. {
  13. //head;
  14. WordNode h("hey");
  15.  
  16. };
  17.  
  18.  
Mar 23 '07 #1
Share this Question
Share on Google+
7 Replies


DeMan
100+
P: 1,806
I think part of the problem, is that both of your .h files use the same macro RANDOM to avoid being used more than once.....

You may be better to replace RANDOM (in both files I think - try to use obscure names that can't comde up in different circumstances) so that (for example) you could have:

BST.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef _BST_H
  2. #define _BST_H
  3.  
  4. /* Declare protoypes etc */
  5.  
  6. #endif
  7.  
and in WordNode.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef _WORDNODE_H
  2. #define _WRODNODE_H
  3.  
  4. /* Declare things */
  5.  
  6. #endif
  7.  
Mar 23 '07 #2

P: 9
I'm trying to make a binary search tree for strings, and I've barely started but I already hit a wall. I've programmed in Java for a year or so and recently made the switch to C++ (for a class, college student). Anyway, I have WordNode.h and WordNode.cpp. Alone, they work fine (I decided not to use a struct, I'll need more flexiblity for this problem). But when I try and use a WordNode inside the BST.h and BST.cpp, it gives me the following error:

16 \BST.h `WordNode' does not name a type

Here are the files
Expand|Select|Wrap|Line Numbers
  1. WordNode.h
  2.  
  3.  
  4. #ifndef WORDNODE_H
  5. #define WORDNODE_H
  6. #include <string> 
  7. #include <iostream>
  8. #include "BST.h"
  9.  
  10. using namespace std;
  11.  
  12. class WordNode 
  13.   public:
  14.          WordNode();
  15.     WordNode(string name);
  16.     string word;
  17.     void getLeft( WordNode);
  18.     void getRight (WordNode); 
  19.     WordNode *left;
  20.     WordNode *right;
  21.     bool hasLeft;
  22.     bool hasRight;
  23.     friend ostream& operator<<(ostream& os, const WordNode& wn);
  24.  
  25. }; 
  26.  
  27. #endif
  28.  
  29.  
  30. WordNode.cpp
  31.  
  32. #include <string> 
  33. #include <iostream>
  34. #include "WordNode.h"
  35.  
  36. using namespace std;
  37.  
  38. WordNode::WordNode()
  39. {
  40. }
  41.  
  42. WordNode::WordNode( string w)
  43. {
  44.           word = w;                    
  45. }
  46.  
  47. void WordNode::getLeft( WordNode lptr)
  48. {
  49.      hasLeft = true;
  50.           left = &lptr;
  51. }
  52.  
  53.  
  54.  
  55.  
  56. void WordNode::getRight( WordNode rptr)
  57. {
  58.      hasRight = true;
  59.           right = &rptr;
  60. }
  61.  
  62.  
  63.  
  64.  ostream& operator<<(ostream& os, const WordNode& wn)
  65. {
  66.                 os << wn.word;
  67.                 return os;
  68. };
  69.  
  70.  
  71. BST.h
  72.  
  73. #ifndef BST_H
  74. #define BST_H
  75.  
  76. #include <string> 
  77. #include <iostream>
  78. #include "BST.h"
  79. #include "WordNode.h"
  80.  
  81. using namespace std;
  82.  
  83. class BST
  84. {
  85.       public:
  86.              BST();
  87.              WordNode head; <------------------------------------ERROR
  88. };
  89.  
  90. #endif
  91.  
  92.  
  93. BST.cpp
  94.  
  95. #include <string> 
  96. #include <iostream>
  97. #include "BST.h"
  98. #include "WordNode.h"
  99.  
  100. using namespace std;
  101.  
  102. WordNode head("hey");
  103.  
  104. BST::BST()
  105. {                 
  106. };
  107.  
  108.  
Any help would be greatly appreciated, and please go easy, I've only made 1 semi-difficult C++ program and about a dozen very easy ones (but none had two classes and two headers).
Mar 23 '07 #3

DeMan
100+
P: 1,806
Please do not double post....
Your previous post can be found here

If thwe answer provided does not sufficiently help, pos again being clear about where your difficulty lies
Mar 23 '07 #4

RedSon
Expert 5K+
P: 5,000
Merged .
Mar 23 '07 #5

P: 9
Very sorry, I'm new but I know double posts suck. I thought this problem was different enough and new title might help.
Mar 23 '07 #6

DeMan
100+
P: 1,806
Could you please highlight the difference in the two queries. Sorry I assumed they were both the same I can see some differences on more close inspection. As a general rule, if we are still talking about the same program, we'll keep it in the same thread.
If you need something totally off topic, you can open a new thread
Mar 24 '07 #7

DeMan
100+
P: 1,806
I don't think this is your problem, but you seem to be including BST.h in itself.....

I think you should be creating the WrodNode something like :

Expand|Select|Wrap|Line Numbers
  1. WordNode head = new WordNode("hey");
  2.  
Mar 24 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.