ii69outof247
ii69outof247

Reputation: 23

How do I fix my object initialization for a template BST class?

This problem occurs in my main.cpp:

using namespace std;

#include <iostream>

#include "BST.h"
#include "Packet.h"

int main()
{
    BST test; // It occurs on this line!
    Packet one(1, "testPacket", 1, 1);

    system("Pause");
}

The error on that line says:

argument list for class template "BST" is missing

I don't know how to fix it. I just want to initialize the BST. How can I fix this error? I'm not very experienced with templates. Please help. My priority is fixing this glaring problem right now. Can I get help?

For reference purposes:

BST.h:

#ifndef BST_H
#define BST_H

using namespace std;

template <typename T>
class Node {
    public:
        Node() : rlink(nullptr), llink(nullptr) {}
        ~Node() {}
    private:
        T data;
        Node *rlink, *llink;

};

template <typename T>
class BST {
public:
    BST();
    void insert(T data);
private:
    Node * root; 
};

#endif

BST.cpp

#include "BST.h"

template <typename T>
BST<T>::BST() : root(nullptr) {}

template <typename T>
void BST<T>::insert(T data) {
    if (root != nullptr) {

    }
    else {
        cout << "NPTR" << endl;
    }
}

Packet.h

#ifndef PACKET_H
#define PACKET_H

#include <string>

using namespace std;

class Packet {
public:
    Packet(int partId, string description, double price, int partCount) :
        partId(partId), description(description), price(price), partCount(partCount) {}
    int getPartId() const { return partId; }
    string getDescription() const { return description; }
    double getPrice() const { return price; }
    int getPartCount() const { return partCount; }
private:
    int partId;
    string description;
    double price;
    int partCount;
};

#endif

Upvotes: 1

Views: 132

Answers (1)

user10957435
user10957435

Reputation:

There are 2 problems.

The first is that Node needs to know what type T is, so you need to tell it when you use Node like this:

template <typename T>
class BST {
public:
    BST();
    void insert(T data);
private:
    Node<T> * root; 
};

Secondly, BST needs to know what its own type T is when you try to use it, so you need to do it like this:

BST<int> test; // Or whatever you are searching for in your tree. Doesn't have to be an int

P.S. Just heading this off now, you're probably going to need to implement BST in the header file. Failure to do so might cause linker problems.


P.P.S. I've been reading your comments on the original post, and what you actually probably need this instead:

BST<Packet> test; // Since you are searching for packets.

Upvotes: 2

Related Questions