1

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
  • 2
    several issues, should be something like `BST test;`. then read [why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Jarod42 Jul 25 '19 at 00:05
  • But I'm not trying to have BST handle any int. It's just a constructor for creation of the BST which is a binary search tree object. Why then would I use BST test;??? – ii69outof247 Jul 25 '19 at 00:15
  • In your code, `BST` is a template class, so to instantiate it, you need to provide its type parameter. (you can instantiate `BST` for a given `T`, but `BST` is not a class and cannot be instantiated). – Jarod42 Jul 25 '19 at 00:25
  • 1
    My intention is to do BST test and then being able to do test.insert(one) which is the Packet "one" as seen in main.cpp. Is there any way to achieve this with the flexibility of a template? I'm not sure how to achieve that? – ii69outof247 Jul 25 '19 at 00:36
  • Please avoid `using namespace std;`. It is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) In particular, don't use it before `#include `. – L. F. Jul 25 '19 at 00:48
  • I've been aware it's bad practice. But my priority is fixing what's the glaring problem in this post. Do you have any ideas on how to fix this so I can use it as my intention states? Because if this isn't fixed, I don't think I can use my BST to take in Packet. – ii69outof247 Jul 25 '19 at 00:50
  • 1
    So it seems you want `BST test;`. – Jarod42 Jul 25 '19 at 00:53
  • 1
    `BST` is a template that takes a parameter, and you must provide one to instantiate the template, and you need to figure out what that template parameter must be. That's the only way that C++ templates work. Either that, or `BST` should not be a template. – Sam Varshavchik Jul 25 '19 at 01:51

1 Answers1

2

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.
  • Thank you. You solved my problem. Instead of putting everything in the header, I opted to include the .h and .cpp in my main.cpp which solves the problem too! For instance #include "BST.cpp" and of the course the .h file! – ii69outof247 Jul 25 '19 at 22:12
  • [Here's why that's not a good idea](https://stackoverflow.com/q/19547091/10957435), but whatever works for you, I guess. –  Jul 26 '19 at 00:46