1

I write a code to implement a circular list using a template:

    template <class T>
    class CircularList {
        //implement
    }
    int main() {
        CircularList<string>* letters = new CircularList<>();
    }

But I have an error when I create an instance in the main class, what is the right syntax to write this?

Xavi
  • 206
  • 6
  • 4
    1. You're missing `()` after `main`. 2. You need to type full type name - `new CircularList();` (you could omit type name on left hand side by using `auto`). 3. Don't use `new`. – Yksisarvinen Apr 16 '21 at 21:24
  • 3
    On point 3. `CircularList letters;` should be all that's required to get a working object that is automatically allocated and has an automatically managed lifetime. The more you can get the system to automatically do for you, the easier C++ programming gets. – user4581301 Apr 16 '21 at 21:27
  • @user4581301 What about the * – Xavi Apr 16 '21 at 21:32
  • That's if you still use `new`. If you do as @user4581301 suggests, you should remove `*`. – Ted Lyngmo Apr 16 '21 at 21:33
  • 3
    @Xavi You don't want pointers, unless you really need to. They are difficult to use safely and properly. Whenever you can, you want objects, not pointers. Automatic lifetime is your friend. When you do need pointers, you also don't want to use raw pointers with `*` and `new` - you want smart pointers like `std::unique_ptr`. But the default choice is always an automatic variable. – Yksisarvinen Apr 16 '21 at 21:34
  • 1
    The `*` says you have a pointer to a `CircularList`, rather than a `CircularList`. If you have a pointer, that pointer must point to a valid `CircularList` in order to be used as a `CircularList`. You don't have to point at an object dynamically allocated with `new`, but you must get a `CircularList` from somewhere. Pointers are very useful. Dynamic allocation is very useful, but you have to chose the right time and place to use them or they make you pay with extra work. – user4581301 Apr 16 '21 at 21:37
  • 2
    Handy reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Apr 16 '21 at 21:38

2 Answers2

2

You can do it your way like this:

template <class T>
class CircularList {
    //implement
}; //don't forget the ; after classes

int main() {
    CircularList<string>* letters = new CircularList<string>; //we still have to put string in the template. This calls the default constructor, and assings the result to a string
}

It is normally discouraged to use new. I would recommend just doing this:

template <class T>
class CircularList {
    //implement
};

int main() {
    CircularList<string> letters; //still calls default constructor, but no need for new or pointers
}
1

You have a problem here:

new CircularList<>()

<> you did not specify template type

So your solution is:

new CircularList<std::string> ();
KPCT
  • 3,815
  • 2
  • 16
  • 34
  • I think you need to expand on point 1 because either I'm misunderstanding you or you are wrong. `new CircularList();` is valid – user4581301 Apr 16 '21 at 23:14
  • @user4581301 tnx for your suggestion. I screwed up. It was a long day yesterday. I was thinking of `Type* type_t ()`, this does not call constructor, but rather declares a function. – KPCT Apr 17 '21 at 14:21