-1

I have my own LinkedList class, and when I'm creating an instance in the main i get the "expression must have class type" error. I have a default c'tor in the LinkedList class.

so when I'm trying to do this :

    LinkedList<Animal> aL();

    for (int i = 0; i < numOfAnimals; i++)
    {

        aL.addLast(*animals[i]);
    }

    cout << aL << endl;

it won't compile. but if I declare like this:

    LinkedList<Animal> aL = LinkedList<Animal>();

it works. of course that the secont option isn't efficient, and i rather use the first one. can anyone explain me why it doesn't work or how to fix it? Thnaks!

Serjio
  • 23
  • 5

1 Answers1

2

The line

LinkedList<Animal> aL();

is not creating an instance of a LinkedList<Animal>, but is declaring a function aL with no arguments and LinkedList<Animal> as its return type.

To create an object using the default constructor, just don't use any braces:

LinkedList<Animal> aL;

Alternatively, if you wish to be that explicit, you can use curly braces from C++11

LinkedList<Animal> aL{};

Or, if you like function call syntax that much, you can

LinkedList<Animal> aL = LinkedList<Animal>();

or even

auto aL = LinkedList<Animal>();
lisyarus
  • 15,025
  • 3
  • 43
  • 68