soandos
soandos

Reputation: 5146

VS 2011 template class

I have a rather standard situation where I want to use a templated class in the following way:

  1. Define a .h file
  2. Have it include the .cpp

In every other compiler that I try (i.e. g++ and clang/llvm) this works fine. In visual studio, it tells me that the file has already been defined.

If I manually cut and paste the text from the .cpp into the .h file, then everything works out just fine. I was under the impression that that was exactly what #include was supposed to do.

My hunch is that visual studio is compiling the .cpp file more than once somehow (though I placed #pragma once on the .h and .cpp files).

What is going on, and how can I make my template classes behave in VS?

Code follows:
.h:

#pragma once
template <class T>
class myVector
{
private:
    void grow();
public:
int size;
int index;
T** words;
void pushBack(T* data);
inline T* operator[](int);
myVector(void);
~myVector(void);
};

#include "myVector.cpp"

.cpp:

#pragma once
#include "stdafx.h"
#include <cstdlib>
#include "myVector.h"
#include <iostream>
using namespace std;

template<class T>
myVector<T>::myVector(void)
{
    this->size = 2000;
    words = new T*[size];
    index=0;
}

template<class T>
void myVector<T>::pushBack(T* input)
{
    if(index<size)
    {
        words[index]=input;
    }
    else
    {
        grow();
        words[index]=input;
    }
    index++;
}

template<class T>
T* myVector<T>::operator[](int i)
{
    return words[i];
}

template<class T>
void myVector<T>::grow()
{
    //cout<<"I grew:"<<endl;
    size*=2;
    words = (T**)realloc(words,size*sizeof(T*));
}

template<class T>
myVector<T>::~myVector(void)
{
    delete[] words;
}    

Upvotes: 0

Views: 114

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258618

It seems to me that your confusion arises from not knowing how #pragma once and translation units work.

#pragma once, much like include guards, prevent the contents of a file (usually a header) from being pulled into a single translation unit more than once.

If you #include <vector> in multiple implementation files, the contents will be pulled in all of them, but only once for each translation unit.

So you should remove the #include "myVector.cpp", as MSVS automatically compiles implementation files, and it's also wrong.

Note that template definitions have to be visible, so you'll need to either move them to the .h file, like you did, or, using your current approach, rename the .cpp file to something like .impl or even .h and include it.

Upvotes: 2

Related Questions