-2

I am trying to understand the Cpp reference document - https://en.cppreference.com/w/cpp/container/vector

I saw that the signature of vector is

template<
    class T,
    class Allocator = std::allocator<T>
> class vector;

My understanding is that class T is allowing the vector to pass the type it wants like vector, vector, vector, etc. So, the compiler should do the magic for the primitive types but for custom class types, I am sure I might have to implement some copy constructor, etc. Basically, class T means the type of vector here.

I thought to make something similar in nature but I get this error.

#include <iostream>

template<class T> class myExp;

int main() {
    
    myExp<int> t;

    return 0;
}

I am sure I am missing some template fundamentals here. When I compile I get the error -

/tmp/fRK0p8KnAQ.cpp:10:16: error: aggregate 'myExp<int> t' has incomplete type and cannot be defined
   10 |     myExp<int> t;
      |                ^

1 Answers1

1

You missing things up. In your example, the myExp is an incomplete type, so you cannot have objects of type myExp.

Here's a simple example:

// We do define the myExp type here
template<class T> class myExp
{
    T data;
    // ...
};

int main () 
{
    // So now we can instantiate objects of myExp type
    myExp<int> foo;
}

You may want to take a look in reference and educational materials to get a better grasp on these concepts.

rawrex
  • 3,929
  • 2
  • 7
  • 22