0

This is the header file code that I am having issues with. I have these two errors:

PCH WARNING: header stop not at file scope. An IntelliSense PCH file was not generated.

LNK1168 cannot open file for writing.

I've tried adding #pragma once and moving the #endif to the bottom, yet nothing works. I am a beginner at coding so excuse me for the messy code. I Would appreciate if someone could help me with this issue.

#pragma once
#ifndef BST_H
#define BST_H

#include <vector>
#include <string>

template <class T>
class BST
{
private:
    class Node
    {
    public:
        T element;
        Node* left;
        Node* right;
        Node(T element);
        ~Node() {};
    };

    Node* root;
    void ToGraphvizHelper(std::string& listOfNodes, std::string& listOfConnections, Node* toWorkWith, size_t& uniqueID);


public:
    BST();
    ~BST();
    void insert(T element);
    void remove(T element);
    bool find(T element);
    std::vector<T> inOrderWalk();
    std::vector<T> preOrderWalk();
    std::vector<T> postOrderWalk();
    int getTreeHeight();
    T getMin();
    T getMax();
    std::string ToGraphviz();
};
template <class T>
std::string BST<T>::ToGraphviz() // Member function of the AVLTree class
{
    std::string toReturn = "";
    if (this->root) // root is a pointer to the root node of the tree
    {
        std::string listOfNodes;
        std::string listOfConnections = std::string("\t\"Root\" -> ") + std::to_string(0) + std::string(";\n");
        toReturn += std::string("digraph {\n");
        size_t id = 0;
        ToGraphvizHelper(listOfNodes, listOfConnections, root, id);
        toReturn += listOfNodes;
        toReturn += listOfConnections;
        toReturn += std::string("}");
    }
    return toReturn;
}

template <class T>
void BST<T>::ToGraphvizHelper(std::string& listOfNodes, std::string& listOfConnections, Node* toWorkWith, size_t& uniqueID) // Member function of the AVLTree class
{
    size_t myID = uniqueID;
    listOfNodes += std::string("\t") + std::to_string(myID) + std::string(" [label=\"") + std::to_string(toWorkWith->element) + std::string("\"];\n");
    if (toWorkWith->left)
    {
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID + 1) + std::string(" [color=blue];\n");
        ToGraphvizHelper(listOfNodes, listOfConnections, toWorkWith->left, ++uniqueID);
    }
    else
    {
        listOfNodes += std::string("\t") + std::to_string(++uniqueID) + std::string(" [label=") + std::string("nill, style = invis];\n");
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID) + std::string(" [ style = invis];\n");
    }

    if (toWorkWith->right)
    {
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID + 1) + std::string(" [color=red];\n");
        ToGraphvizHelper(listOfNodes, listOfConnections, toWorkWith->right, ++uniqueID);
    }
    else
    {
        listOfNodes += std::string("\t") + std::to_string(++uniqueID) + std::string(" [label=") + std::string("nill, style = invis];\n");
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID) + std::string(" [ style = invis];\n");
    }
}
#endif //BST_H
template<class T>
inline BST<T>::BST()
{
}

template<class T>
inline BST<T>::~BST()
{
}

template<class T>
inline void BST<T>::insert(T element)
{
    if (root == nullptr)
    {
        root = new Node(element);
    }
    else
    {
        Node* current = root;
        Node* parent = nullptr;
        while (current != nullptr)
        {
            parent = current;
            if (element == current->element)
            {
                return;
            }
            else if (element < current->element)
            {
                current = current->left;
            }
            else
            {
                current = current->right;
            }
        }
        if (element < parent->element)
        {
            parent->left = new Node(element);
        }
        else
        {
            parent->right = new Node(element);
        }
    }
}
template<class T>
inline void BST<T>::remove(T element)
{
}

template<class T>
inline bool BST<T>::find(T element)
{
}

template<class T>
inline std::vector<T> BST<T>::inOrderWalk()
{
    return std::vector<T>();
}

template<class T>
inline std::vector<T> BST<T>::preOrderWalk()
{
    return std::vector<T>();
}

template<class T>`
inline std::vector<T> BST<T>::postOrderWalk()
{
    return std::vector<T>();
}

template<class T>
inline int BST<T>::getTreeHeight()
{
    return 0;
}

template<class T>
inline T BST<T>::getMin()
{
    Node* current = root;
    while (current->left != nullptr)
    {
        current = current->left;
    }

    return current->element;
}

template<class T>
inline T BST<T>::getMax()
{
    Node* current = root;
    while (current->right != nullptr)
    {
        current = current->right;
    }
    
    return current->element;
}

template <typename T>
BST<T>::Node::Node(T element) : element(element), left(nullptr), right(nullptr)
{
}
#pragma once
#ifndef BST_H
#define BST_H

#include <vector>
#include <string>

template <class T>
class BST
{
private:
    class Node
    {
    public:
        T element;
        Node* left;
        Node* right;
        Node(T element);
        ~Node() {};
    };

    Node* root;
    void ToGraphvizHelper(std::string& listOfNodes, std::string& listOfConnections, Node* toWorkWith, size_t& uniqueID);


public:
    BST();
    ~BST();
    void insert(T element);
    void remove(T element);
    bool find(T element);
    std::vector<T> inOrderWalk();
    std::vector<T> preOrderWalk();
    std::vector<T> postOrderWalk();
    int getTreeHeight();
    T getMin();
    T getMax();
    std::string ToGraphviz();
};
template <class T>
std::string BST<T>::ToGraphviz() // Member function of the AVLTree class
{
    std::string toReturn = "";
    if (this->root) // root is a pointer to the root node of the tree
    {
        std::string listOfNodes;
        std::string listOfConnections = std::string("\t\"Root\" -> ") + std::to_string(0) + std::string(";\n");
        toReturn += std::string("digraph {\n");
        size_t id = 0;
        ToGraphvizHelper(listOfNodes, listOfConnections, root, id);
        toReturn += listOfNodes;
        toReturn += listOfConnections;
        toReturn += std::string("}");
    }
    return toReturn;
}

template <class T>
void BST<T>::ToGraphvizHelper(std::string& listOfNodes, std::string& listOfConnections, Node* toWorkWith, size_t& uniqueID) // Member function of the AVLTree class
{
    size_t myID = uniqueID;
    listOfNodes += std::string("\t") + std::to_string(myID) + std::string(" [label=\"") + std::to_string(toWorkWith->element) + std::string("\"];\n");
    if (toWorkWith->left)
    {
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID + 1) + std::string(" [color=blue];\n");
        ToGraphvizHelper(listOfNodes, listOfConnections, toWorkWith->left, ++uniqueID);
    }
    else
    {
        listOfNodes += std::string("\t") + std::to_string(++uniqueID) + std::string(" [label=") + std::string("nill, style = invis];\n");
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID) + std::string(" [ style = invis];\n");
    }

    if (toWorkWith->right)
    {
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID + 1) + std::string(" [color=red];\n");
        ToGraphvizHelper(listOfNodes, listOfConnections, toWorkWith->right, ++uniqueID);
    }
    else
    {
        listOfNodes += std::string("\t") + std::to_string(++uniqueID) + std::string(" [label=") + std::string("nill, style = invis];\n");
        listOfConnections += std::string("\t") + std::to_string(myID) + std::string(" -> ") + std::to_string(uniqueID) + std::string(" [ style = invis];\n");
    }
}
#endif //BST_H
template<class T>
inline BST<T>::BST()
{
}

template<class T>
inline BST<T>::~BST()
{
}

template<class T>
inline void BST<T>::insert(T element)
{
    if (root == nullptr)
    {
        root = new Node(element);
    }
    else
    {
        Node* current = root;
        Node* parent = nullptr;
        while (current != nullptr)
        {
            parent = current;
            if (element == current->element)
            {
                return;
            }
            else if (element < current->element)
            {
                current = current->left;
            }
            else
            {
                current = current->right;
            }
        }
        if (element < parent->element)
        {
            parent->left = new Node(element);
        }
        else
        {
            parent->right = new Node(element);
        }
    }
}
template<class T>
inline void BST<T>::remove(T element)
{
}

template<class T>
inline bool BST<T>::find(T element)
{
}

template<class T>
inline std::vector<T> BST<T>::inOrderWalk()
{
    return std::vector<T>();
}

template<class T>
inline std::vector<T> BST<T>::preOrderWalk()
{
    return std::vector<T>();
}

template<class T>
inline std::vector<T> BST<T>::postOrderWalk()
{
    return std::vector<T>();
}

template<class T>
inline int BST<T>::getTreeHeight()
{
    return 0;
}

template<class T>
inline T BST<T>::getMin()
{
    Node* current = root;
    while (current->left != nullptr)
    {
        current = current->left;
    }

    return current->element;
}

template<class T>
inline T BST<T>::getMax()
{
    Node* current = root;
    while (current->right != nullptr)
    {
        current = current->right;
    }
    
    return current->element;
}

template <typename T>
BST<T>::Node::Node(T element) : element(element), left(nullptr), right(nullptr)
{
}

raiyan22
  • 1,043
  • 10
  • 20

1 Answers1

1

#endif //BST_H - this is a header stop. It's not at the file end. Move it to at the file end. Note, the error doesn't disappear immediately, IntelliSense is not a realtime tool and runs with timeout intervals.

#pragma once is supported by all significant C++ compilers for many years, you can safely stop using header guards and remove #ifndef BST_H, #define BST_H and #endif //BST_H.

273K
  • 29,503
  • 10
  • 41
  • 64
  • I've now removed the header guards and inserted a #pragma once at the top but it is still generating the same error that in the bottom of the code its giving off "PCH warning: header stop not at file scope. An IntelliSense PCH file was not generated. It is also telling me that it cannot open file \Debug\filename.exe – Konstigt May 07 '23 at 16:44
  • Note, the error doesn't disappear immediately, IntelliSense is not a realtime tool and runs with timeout intervals. You may try to rebuild the project. – 273K May 07 '23 at 16:44
  • Are you sure this is the responsible header? You could also delete the precompiled header files so they are recreated from scratch. – Sebastian May 07 '23 at 16:47