Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:50:57+00:00 2026-06-03T07:50:57+00:00

I have a rather standard situation where I want to use a templated class

  • 0

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;
}    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:50:58+00:00Added an answer on June 3, 2026 at 7:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather theoretical question. My company has a working standard (documented) that
Have a rather simple question. Does anyone knows if i can use jparallax both
I have a rather complicated (and very inefficient) way of getting utilisation from a
I have a rather large SQL file which starts with the byte order marker
I have a custom image which I would like to use rather than the
Is there a way to add a method to a standard Java class, such
The Setup I have a (rather standard) form that allows a user to create
I have a situation where a rather clever website updates the latest information on
I have rather large project that uses ICU regex classes. Basically it might run
I have some rather odd behavior in my D program that I've narrowed down

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.