Thread View: comp.os.os2.programmer.misc
1 messages
1 total messages
Started by Jon Santarelli
Sun, 08 Nov 1998 00:00
var_decl and templates in C++
Author: Jon Santarelli
Date: Sun, 08 Nov 1998 00:00
Date: Sun, 08 Nov 1998 00:00
93 lines
3565 bytes
3565 bytes
Hi all.
I have been working on a homework problem in and I have received
several errors, well actually the same error in three places. I am
using GNU 2.7.2 under OS/2.
The error is:
tstbinary_node.cpp:42: sorry, not implemented: use of `var_decl' in
function template.
Now I did get it to compile and execute with the correct results,
however, I am not sure if what I did was what the professor wanted.
What I changed to get it to work is line 42 to read
"preorder(root,cout)" and line 75 to "void preorder(binary_node<T>
*p, ostream& os)". The program is for binary trees.
I do hope that someone can give me tell me if this just will not
work as the professor has requested it to, and why, or if I just did
something incorrect in the code.
My code looks like: ( I removed some lines to shorten the post).
--------------------------------------------------
The main program (tstbinary_node)
#include <iostream.h>
#include <cstdlib>
#include "binary_node.h" //..... This is what I wrote.
int main(){
binary_node<int> *root = NULL;
int n;
cout << "\nInsert them: ";
while(cin >> n) {
bst_insert(root, n);
}
cout << "\nPreorder:" << endl;
preorder(root); // This is line 42 Error here................
return EXIT_SUCCESS;
}
--------------- binary_node.h----------------
template <class T>
class binary_node {
public:
binary_node *left, *right;
T data;
// Constructors
binary_node() : left(0), right(0), data(T()) {}
binary_node(const T& n) : left(0), right(0), data(n) {}
};
//==================================================================
===
template <class T>
void bst_insert(binary_node<T>*& p, const T& n){
if (!p) { // Root node?
p = new binary_node<T>(n);
}
else {
if (p->data > n) {
if ( p->left != NULL) // More nodes to the left?
bst_insert(p->left, n);
else // Insert node here.
p->left = new binary_node<T>(n);
} else if (p->data < n) {
if ( p->right != NULL) // More nodes to the
right?
bst_insert(p->right, n);
else // Insert node here.
p->right = new binary_node<T>(n);
} // if (p->data == n) is just ignored!
}
return;
}
template <class T>
void preorder(binary_node<T> *p, ostream& os=cout) { // This is line
75....
if (p!=NULL) {
cout << p->data << endl;
preorder(p->left, cout);
preorder(p->right, cout);
}
return;
}
---------------------------------------------------
Jon Santarelli
jsant@interaccess.com
http://homepage.interaccess.com/~jsant
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads