My own template vector class. Can't push_back?

I'm not too sure why this isn't working. This is the most I've ever used templates so I don't know I messed anything up.

In main:
1
2
3
4
std::string str1 = "Asdf";
...
MyVector<std::string> myV();
myV.push_back(str1);


push_back method:
1
2
3
4
5
6
7
8
9
10
11
template <typename T>
void MyVector<T>::push_back(T const& entry)
{
    if(size == capacity)
    {
        reallocate();
    }

    data[_size] = entry;
    _size++;
}


I'm getting a
request for member 'push_back' in 'myV' which is of non-class type
MyVector<std::string> myV();

Aaahh the old - that is not the way to declare an object using default constructor error
Ah I hate that! I don't why I always do that whenever I come back to C++ -_-

I'm now getting a few undefined references (two to my destructor, one to my constructor, and one to the method being used). Why is this? It's acting like it doesn't have a definition to use for the data types in question, but the constructor is explicitly an int (using the non-default now), and the method takes a template parameter.

EDIT:
This seems to be a pain if I'm reading this right. http://stackoverflow.com/questions/8752837/undefined-reference-to-template-class-constructor

From the sounds of this, templates only work on types that I specify, or I have to shove the implementation in the header file?
Last edited on
¿why is that a problem?
Easiest method is to have it all - including implementation - in the header file.
¿why is that a problem?

It just seems ugly. But I guess this is normal?
You can try #include ing the cpp file at the end of the header if you really want to split the implementation.
If you want to use a templates method outside the file it is implemented .cpp.

Then you need to move the implementation to the header file for which it is declared

OR

You need to use template instantiation inside the cpp file.
Just sticking it all in the header file has been working fine actually. I got used to it.
closed account (S6k9GNh0)
Code has to be generated for each type passed into a template. Thus, you can't compile a template function unless you specify what types you require. Implementing an incline template allows this to happen based on what types you're passing it rather than what types you specify.
Topic archived. No new replies allowed.