-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
Cole Dapprich
  • 33
  • 1
  • 7
  • 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](http://stackoverflow.com/questions/18914106/what-is-the-use-of-using-namespace-std). 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. – Sam Varshavchik Jul 23 '16 at 16:28
  • Thanks for the sentiment, but this doesn't exactly help me solve my problem. – Cole Dapprich Jul 23 '16 at 16:29
  • @Sam Varshavchik Was about to post more or less exactely that. You beat me to it. – Jesper Juhl Jul 23 '16 at 16:30
  • 2
    please post a [MCVE], `Comparable` is nowhere define and hence your code cannot be compiled at all – m.s. Jul 23 '16 at 16:30
  • 2
    Your problem is not this assignment. Your problem is a bad instructor. You can solve this problem by switching classes. Your instructor does not really know anything about C++. Just because your instructor can write compilable code does not mean that the instructor knows C++. – Sam Varshavchik Jul 23 '16 at 16:31
  • 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. – Cole Dapprich Jul 23 '16 at 16:33
  • still not working, `Comparable` is still there – m.s. Jul 23 '16 at 16:35
  • Sorry, didn't see the second occurrence. Should work now – Cole Dapprich Jul 23 '16 at 16:35
  • 1
    Yes, and the code compiles without issues, now that Comparable is removed. No compilation errors, whatsoever. This question cannot be salvaged. – Sam Varshavchik Jul 23 '16 at 16:35
  • Ok, then I just added the Comparable back in how it was originally with a definition this time. Still no errors? – Cole Dapprich Jul 23 '16 at 16:37
  • Why don't you try compiling exactly what you posted here, yourself, and see if there are any compilation errors, then report back. stackoverflow.com is not a code compilation service. If you don't bother checking that your question is valid, as asked, why should anyone bother trying to help you? – Sam Varshavchik Jul 23 '16 at 16:37
  • I just tried compiling again. Getting the exact same errors I listed in my question. – Cole Dapprich Jul 23 '16 at 16:39
  • I *really* dislike that header. Using `NULL` rather than `nullptr`. Using raw pointers rather than smart pointers (if it is supposed to own nodes). That copy ctor is odd; it first initializes `root` then replaces the entire object, what's the point? The indentation is inconsistent as is placement of `&` and `*` etc. And of course the `using namespace std;` in a *header* even. – Jesper Juhl Jul 23 '16 at 16:41

1 Answers1

1
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.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • 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. – Cole Dapprich Jul 23 '16 at 16:56