user3136756
user3136756

Reputation: 11

Using templates with C++

I am trying to implement a red black tree using templates. The insert function takes two generic types, Item and Key. However, when I'm creating an instance of the RedBlackTree in the main() and calling the function 'InsertKey', the program gives the error: Method 'InsertKey' could not be resolved. Also, I don't know what to pass as arguments in the 'InsertKey' function. I implemented an array which consists of random elements. The array should be one of the parameters, but I can't figure out what is the other parameter.

This is my header file:

#ifndef REDBLACKTREE_H_
#define REDBLACKTREE_H_

template <class Item, class Key>
class RedBlackTree
{
    typedef enum
    {
        BLACK,
        RED
    }ColourNode;

    /* user data stored in tree */
    typedef struct {
        int data;
    } treedata;

    typedef struct RBT
    {
        struct RBT *left;
        struct RBT *right;
        struct RBT *parent;
        struct RBT *root;
        ColourNode colour;
        //Item item;
        Key key;
        treedata data;
    }RBTNode;

    public:
        ~RedBlackTree(); // destructor
        RedBlackTree(Item, Key); // default constructor

        void InsertKey(const Item *&, const Key *&);
        void FixingInsert(const Item *&, const Key *&);
        int RemoveKey(Item, Key);
        int FindKey(Item, Key);

    //private:
        //RedBlackTree<Item, Key> *rootPointer;

};

#endif /* REDBLACKTREE_H_ */

This is my main()

#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "RedBlackTree.h"

using namespace std;

int main(int argc, const char* argv[])
{

    const int arraysize = 200;
    int arr[arraysize];

    RedBlackTree<int, int> t1(int, int);

    srand((unsigned)time(0));
    for(int i = 0; i <= arraysize-1;  i++)
    {
        arr[i] = rand() % 210;
        //printf("%d ", arr[i]);
    }

    for(int i = 0; i <= arraysize-1;  i++)
    {
        t1.InsertKey(arr[i], // something else//); //InsertKey should have another parameter, but for now I am trying to figure out why it cannot be resolved. 
    }

}

Also, any ideas of what the something else could be please? I can't figure out what to pass.

Upvotes: 0

Views: 123

Answers (3)

user1508519
user1508519

Reputation:

RedBlackTree<int, int> t1(int, int); is being treated as a function declaration. You haven't defined a default constructor for your RedBlackTree, so you must either provide one, or construct your object with arguments.

RedBlackTree() {  }

The next problem is your function signature for InsertKey expects two arguments, when you're only giving it one.

void InsertKey(const Item *&, const Key *&);
t1.InsertKey(arr[i], /* something else must go here */)

As well as that, you're passing an int when your function expects an int *&. If you meant to pass these arguments by const-reference, remove the asterisk.

void InsertKey(const Item &, const Key &);

Otherwise, you need to pass a pointer.

t1.InsertKey(&arr[i], /* something else */);

The problem is that it expects a const pointer, so you would have to do something like this:

const int* pointer = &arr[i];
t1.InsertKey(pointer, /* something else */);

Upvotes: 1

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153792

You promised the user a function InsertKey() which takes two pointers. You tried to call InsertKey() with one value instead. That's not going to fly.

As a general recommendation, make sure your data structure works with concrete type before turning it into a template (it is the other way around for algorithms). I'd also recommend that you RedBlackTree<K, V> travels in terms of K and V objects rather than using pointers. If a use really wants to use pointer, the class template could be used by specifying pointer arguments.

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

RedBlackTree<int, int> t1(int, int);

Here you are declaring a function called t1 that returns a RedBlackTree<int, int> and takes two arguments both of type int.

I think you actually want to create a RedBlackTree<int, int> object and call it t1. It has one constructor, that takes an Item and a Key. However, you've commented it as saying it's a default constructor, which it isn't. A default constructor is one that takes no arguments. I think you meant to declare the constructor like so:

RedBlackTree(); // default constructor

Then you can create an object of this type like so:

RedBlackTree<int, int> t1;

You never pass types between normal parantheses like (int, int). They always go between the angle brackets, like <int, int> and after a template name. In this case, the template name is RedBlackTree and we want to instantiate it with int types, so we do RedBlackTree<int, int>. There's nothing to pass to the constructor.

Upvotes: 3

Related Questions