Article View: comp.os.os2.programmer.misc
Article #302var_decl and templates in C++
From: 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
Technical Details
Message-ID:
<722nge$e7n$1@supernews.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,74624fe5a754ea93,start X-Google-Attributes: gid11318c,public X-Google-Thread: 1049cc,74624fe5a754ea93,start X-Google-Attributes: gid1049cc,public X-Google-Thread: 11307a,74624fe5a754ea93,start X-Google-Attributes: gid11307a,public From: jsant@interaccess.com (Jon Santarelli) Subject: var_decl and templates in C++ Date: 1998/11/08 Message-ID: <722nge$e7n$1@supernews.com> X-Deja-AN: 409547915 Distribution: world X-Complaints-To: newsabuse@supernews.com X-Trace: 910483790 Z.COWSAII61B8CC95C usenet58.supernews.com Organization: Posted via Supernews, http://www.supernews.com - Discussions start here! Reply-To: jsant@interaccess.com Newsgroups: comp.os.os2.programmer.misc,comp.os.os2.programmer.tools,gnu.gcc.help