0

I'm a relatively new programmer I'm working on a templated linked list project that can take in multiple linked lists of different data types. I think I've almost got it to work but there is an error that keeps coming up saying that my identifier "m_node" is unidentified. I've researched the error and have found solutions that either aren't relevant to what I'm doing or I've already done, like including the header file for Node in TemplinkedList.h specifically, leaving me thoroughly confused. Here is my TempLinkedList.cpp and Node.h files, apologies if the answer is obvious and thank you in advance if you can crack it.

Edit: Thank you all for your suggestions. I've tried them all out and have managed to resolve the issue. Another error has come up though. This one is called LNK2019 and it says that there is an unresolved external symbol, I assume in my Source.cpp file since it refers to the void main() function. I've tried to fix it but I'm not sure which part of the function is throwing the error.

Source.cpp:

#include "Node.h"
#include "TempLinkedList.h"
template<typename T>
void Testone()
{
    TempLinkedList* list = new TempLinkedList();

    TempNode* m_head1 = new TempNode("2");
    TempNode* m_head2 = new TempNode("4");
    TempNode* m_head3 = new TempNode("6");
    TempNode* m_head4 = new TempNode("8");
    TempNode* m_head5 = new TempNode("10");
    TempNode* m_head6 = new TempNode("12");
    list->PushBack(m_head1);
    list->PushBack(m_head2);
    list->PushBack(m_head3);
    list->PushBack(m_head4);
    list->PushBack(m_head5);
    list->PushBack(m_head6);
    list->Execute();

}
template<typename T>
void main(T TTempNode)
{
    Testone<T>();
    TempNode<T> TTempNode;
    T numAdd = TTempNode.GetNext();

    system("pause");
}

TempLinkedList.cpp:

#include <iostream>

template<typename T>
TempLinkedList<T>::TempLinkedList()
{
    m_head = nullptr;
}

template<typename T>
T TempLinkedList<T>::GetHead()
{
    return nullptr;
}

template<typename T>
void TempLinkedList<T>::PushBack(T data)
{
    TempNode* temp = m_head;
    if (m_head == nullptr)
    {
        m_head = m_node; // error point
        return;
    }
    while (temp->GetNext() != nullptr)
    {
        temp = temp->GetNext();
    }
    temp->SetNext(m_node); // error point
}

template<typename T>
void TempLinkedList<T>::Execute()
{
    TempNode* temp = m_head;
    while (temp != nullptr)
    {
        temp->Execute();
        temp = temp->GetNext();
    }
}

Node.h:

#include <string>
#include <iostream>
using namespace std;
template<typename T>
class TempNode
{
public:
    TempNode(string* data, int* m_node)
    {
        m_node = new T[];
            data = new T[];
    }
    

    TempNode(T data, T next, T m_node);
    ~TempNode();

    void SetNext(T* next);
    T* GetNext();
    T GetData();

    void execute();
private:
    T* data;
    T* next;
    T* m_node;

};
  • What is `TempLinkedList`? Your header only defines a `TempNode` class template – UnholySheep Nov 15 '22 at 23:20
  • Once you figured out this error (which seems to be caused by using a variable in one class when that variable has been defined in a different class), then you have the problem [here](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) to worry about. You are going to have to get rid of that cpp file and move all the code to header files eventually, but get this problem sorted out first. – john Nov 15 '22 at 23:24
  • I believe the bug is `TempLinkedList.cpp` does not include `TempLinkedList.h` however `TempLinkedList.cpp` should not exist and the contents of `TempLinkedList.cpp` should be moved to `TempLinkedList.h` so the include problem would go away by fixing the templates must be implemented in the header problem. Related: [https://stackoverflow.com/questions/495021/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) – drescherjm Nov 15 '22 at 23:27
  • Looking through the code there do seem to be a lot of problems. I think you are being a little optimistic. Some of the class design is wrong, for example `data` should not be a pointer. Some of the code is wrong `m_node = new T[];` is not legal C++ for example. Some of the code doesn't make sense, for example `GetHead` always return `nullptr` and seems to have the wrong return type `T` when it should be `TempNode*`. – john Nov 15 '22 at 23:30
  • Unrelated: Consider hiding the `TempNode` class inside the `TempLinkedList` class as a `private` nested class. This allows you to open up `TempNode` to the only class that can see it and reduce the complexity of the node as well as reduce the coupling between the potential for coupling between `TempLinkedList` and its users. Also make sure `TempNode` and `TempLinkedList` observe [the Rule of Three (and friends)](https://en.cppreference.com/w/cpp/language/rule_of_three) or you're in for a world of hurt when copies are made (thanks to `data` and `m_node` requiring special handling). – user4581301 Nov 15 '22 at 23:32
  • Carrying on further, why are all three data members of `TempNode` typed as `T*`? That's really strange. The obvious definition of `TempNode` is `template TempNode { ... private: T data; TempNode* next; }`. – john Nov 15 '22 at 23:33

0 Answers0