0

I saw a few other articles on a similar topic but I think my situation is different from the other ones.

I'm building a templatized linked list class for a school project and one of the rules of the project is that all the code must be inside the .h file. So I have all of my implementation below my class definition. Here's my class definition followed by a part of the main method that I use for testing. My implementation of the methods is below the main method.

#ifndef LIST342_H_
#define LIST342_H_
#include <string>
#include <iostream>
#include <fstream>
using namespace std;

template <typename T>
class List342 {

public:
    
    struct Node {
        T* data;
        Node* next;
    };


    // Constructors and Destructors
    List342();
    List342(const List342& list);
    ~List342();

    bool isEmpty() const;
    bool Insert(T* obj);
    void DeleteList();
    void Print();
    bool Peek(T target, T& result) const;
    bool Remove(T target, T& result);
    bool Merge(List342& list1);
    bool BuildList(string file_name);

    // Overloaded Operators

    friend ostream& operator<< (ostream& stream, const List342& list);
    List342 operator+(const List342& rhs) const;
    List342& operator+=(const List342& rhs);
    List342& operator=(const List342& list);
    bool operator==(const List342& rhs) const;
    bool operator!=(const List342& rhs) const;

    private:
        Node* head_;

};

template <typename T>
int main() {
    // List of my Personal Tests:
    List342<T> List1;
    string* one = new string("can");
    string* two = new string("apple");
    string* three = new string("diamond");
    string* four = new string("block");
    string* five = new string("angel");
    List1.Insert(one);
    List1.Insert(two);
    List1.Insert(three);
    List1.Insert(four);
    List1.Insert(five);

All of my code is in one .h file except I have a blank .cpp file in the solution explorer. If I remove the .cpp file I no longer get the unresolved external source error, but I get a "Unable to start program" error, and it says the .exe file is not found. Did I implement the templatization wrong? My program was running fine before I templatized it.

If this helps, here's some of my implementation that is directly below my main method:

template <typename T>
List342<T>::List342() {
    head_ = nullptr;
}

template <typename T>
List342<T>::List342(const List342& list) {
    *this = list;
}

template <typename T>
List342<T>::~List342() {
    this->DeleteList();
}


template <typename T>
bool List342<T>::isEmpty() const {
    return (head_ == nullptr);
}
  • move your personal tests to a cpp. main should not be templated... also define your functions in that header. all i see are declarations. stuff being defined below main is not how you should be doing things in a header. – Abel Nov 14 '21 at 03:20
  • Are my function definitions not inside the header? I have everything in the header file? The rules on the assignment say to put "the implementation at the bottom of the file", which I assume means below the class definition part. – huntington06 Nov 14 '21 at 03:30
  • do `List342():head_(){}` not `List343();` ... after main ... `List343::List342()...` it should beat having to put a lot of template and inline crud in places. C++ got over classes having to need their own declarations a long time ago. – Abel Nov 14 '21 at 03:39

0 Answers0