-3

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 <iostream> // NULL, cin, cout
using namespace std;

template <typename Comparable>
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
10
  • 3
    If your instructor provided you with a header file that contains "using namespace std;" you have bigger problems than just this assignment. You have an incompetent instructor. You are doing yourself a disservice, to your future career prospects, by staying in this class. Find another instructor. "using namespace std;". In a header file. Good grief. Run away. As fast as you can. Commented Jul 23, 2016 at 16:28
  • Thanks for the sentiment, but this doesn't exactly help me solve my problem. Commented Jul 23, 2016 at 16:29
  • @Sam Varshavchik Was about to post more or less exactely that. You beat me to it. Commented Jul 23, 2016 at 16:30
  • 2
    please post a minimal reproducible example, Comparable is nowhere define and hence your code cannot be compiled at all Commented Jul 23, 2016 at 16:30
  • Fixed the Comparable issue. Just a defined template that I forgot to change when copying and pasting. Sam, if you're not going to help me with the issue I asked about, please take your bad vibes elsewhere. Commented Jul 23, 2016 at 16:33

1 Answer 1

2
template <typename Comparable>
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<int> &t1, AvlTree<int> &t2)

Now you declared a function that takes two parameters, each one is a reference to an instance of a AvlTree<int>, 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<int> parameters, or AvlTree<char> parameters, or maybe it, itself, should be a template function, is something for you to figure out.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was the issue. I apologize for my lack of knowledge, I've only been coding for 2 years, so this is the first time I've encountered a template. I assumed he was just using it to mask the typename of the element so that I could place whatever I wanted inside; I didn't know I had to explicitly define what I wanted it to take. As for the header file and my instructor's credentials, all I can say is that this is a class on data structures and algorithms, not strictly coding, and I know he primarily works with python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.