1

I am trying to learn c++. So I started creating a simple class Linked List. From what I understood you should put all the methods/contructors declarations in the .h file. But after I have done that I have encountered a problem, while creating the method prepend. How do I use the keyword this in the definitions file (.cpp):

Header File

#ifndef LIST_LIST_H
#define LIST_LIST_H

template <class T>
class List {
public:
    List(T data, List tail): data(data), tail(tail) {}
    List prepend(T data);
    List get(int i);

private:
    List tail;
    T data;
};

#endif //LIST_LIST_H

Implementation File

#include "List.h"

template <class T>
List::List(T data, List tail): data(data), tail(tail) {}

template <class T>
List List::prepend(T data) {
    return new List(data, this);
}

I tried searching for the solution but since I am new to c++ I don't know how to search it.

Collateral.dmg
  • 128
  • 1
  • 10
Samuel Kodytek
  • 1,004
  • 2
  • 11
  • 23
  • 4
    Please learn how to create an [mcve], then add that to the question so it is self contained. (It's a real problem - pastebin is blocked by my employer). To format code, paste it in, select it, then click the `{}` button (or just add 4 spaces before every line before you paste it in - easy in a smart editor, eg `%s/^/ /` in vim). – BoBTFish Jun 03 '16 at 10:38
  • 4
    Please read ["Why can templates only be implemented in the header file?"](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Some programmer dude Jun 03 '16 at 10:40
  • 1
    Also, a good book (see e.g. [this question here for a list](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)) should have told you how to implement templated member functions. – Some programmer dude Jun 03 '16 at 10:42
  • `List List::prepend(T data) {` apart from unfortunately needing to be in the header file as Joachim Pileborg said, you need to do stuff like `List List::prepend(T data) {` as a different type of list is created for every `T`, so which type it is needs to be specified. And such list operations would better need pointers instead of hardcopying everything. If you create a list now, it'll create another one in `tail` which will create another one and so on... until the stack explodes. – coyotte508 Jun 03 '16 at 10:43
  • Your code has a few different errors. (1) [Template classes must be defined in header files](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). (2) A class cannot store a copy of itself. In your example you're trying to store a full copy of your `List` class in inside your `List` class, which is nonsensical. You must store a reference or a pointer to an existing `List` class. (3) Your `tail` member and your `tail` constructor parameter do not have template parameters specified. – Karl Nicoll Jun 03 '16 at 10:51
  • Your data member: List tail; should probably be a pointer: List *tail; – Yotam Jun 03 '16 at 10:58
  • 1
    A `List` can't be a member of `List` because that would make a `List` infinitely large. If you've been coding Java there are quite a few things you need to unlearn. [Here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is a good starting point. – molbdnilo Jun 03 '16 at 11:28

0 Answers0