My instructor provided us with a header file that defines the class AvlTree, but for some reason I can't declare objects normally in main(). What am I doing wrong? Here is the relevant portion of the header file:
#ifndef AVL_TREE_H
#define AVL_TREE_H
#include // NULL, cin, cout
using namespace std;
template
class AvlTree
{
public:
AvlTree( ) : root( NULL )
{ }
AvlTree( const AvlTree & rhs ) : root( NULL )
{
*this = rhs;
}
private:
struct AvlNode
{
Comparable element;
AvlNode *left;
AvlNode *right;
int height;
AvlNode( const Comparable & theElement, AvlNode *lt,
AvlNode *rt, int h = 0 )
: element( theElement ), left( lt ), right( rt ), height( h ) { }
};
AvlNode *root;
};
#endif
Here's what I'm trying to do in main:
#include "AvlTree.h"
void readFile(AvlTree &t1, AvlTree &t2)
{
// do some stuff
return;
}
void splayAccess(AvlTree &t1, AvlTree &t2)
{
// do some stuff
return;
}
int main (void)
{
// object declarations
AvlTree tree1;
AvlTree tree2;
// function calls
readFile(tree1, tree2);
splayAccess(tree1, tree2);
return 0;
}
And here are the errors (GNU compiler):
cwd0042@cse04:~/3110/hw4$ g++ header.h mcve.cpp
mcve.cpp:3:15: error: variable or field ‘readFile’ declared void
mcve.cpp:3:23: error: missing template arguments before ‘&’ token
mcve.cpp:3:24: error: ‘t1’ was not declared in this scope
mcve.cpp:3:36: error: missing template arguments before ‘&’ token
mcve.cpp:3:37: error: ‘t2’ was not declared in this scope
mcve.cpp:9:18: error: variable or field ‘splayAccess’ declared void
mcve.cpp:9:26: error: missing template arguments before ‘&’ token
mcve.cpp:9:27: error: ‘t1’ was not declared in this scope
mcve.cpp:9:39: error: missing template arguments before ‘&’ token
mcve.cpp:9:40: error: ‘t2’ was not declared in this scope
mcve.cpp: In function ‘int main()’:
mcve.cpp:18:10: error: missing template arguments before ‘tree1’
mcve.cpp:18:10: error: expected ‘;’ before ‘tree1’
mcve.cpp:19:10: error: missing template arguments before ‘tree2’
mcve.cpp:19:10: error: expected ‘;’ before ‘tree2’
mcve.cpp:22:11: error: ‘tree1’ was not declared in this scope
mcve.cpp:22:18: error: ‘tree2’ was not declared in this scope
mcve.cpp:22:23: error: ‘readFile’ was not declared in this scope
mcve.cpp:23:26: error: ‘splayAccess’ was not declared in this scope
Answer
template
class AvlTree
// ...
This declares a template class called AvlTree. This template takes one parameter.
void readFile(AvlTree &t1, AvlTree &t2)
The syntax for declaring a function is:
{return type} function-name( {parameter list} )
{parameter list} is an optional list of comma-separated parameters the function. Loosely speaking, each parameter is specified as
{type} {name}
The type of the parameter, followed by its name (again, loosely speaking).
"AvlTree" is not a type. It is a template. To make it a type, you need to provide appropriate template parameters.
void readFile(AvlTree &t1, AvlTree &t2)
Now you declared a function that takes two parameters, each one is a reference to an instance of a AvlTree, which is a type. "AvlTree" by itself is not a type. It's a name of a template.
The same problem is causing all the other compilation errors, here.
Whether ReadFile() should take AvlTree parameters, or AvlTree parameters, or maybe it, itself, should be a template function, is something for you to figure out.
No comments:
Post a Comment