VS 2011 模板类

Posted

技术标签:

【中文标题】VS 2011 模板类【英文标题】:VS 2011 template class 【发布时间】:2012-05-07 07:02:59 【问题描述】:

我有一个相当标准的情况,我想通过以下方式使用模板类:

    定义一个 .h 文件 是否包含 .cpp

在我尝试的所有其他编译器(即 g++ 和 clang/llvm)中,这都可以正常工作。在 Visual Studio 中,它告诉我该文件已被定义。

如果我手动将 .cpp 中的文本剪切并粘贴到 .h 文件中,那么一切正常。我的印象是,这正是 #include 应该做的。

我的预感是 Visual Studio 以某种方式不止一次编译 .cpp 文件(尽管我将 #pragma once 放在 .h 和 .cpp 文件上)。

发生了什么,如何让我的模板类在 VS 中运行?

代码如下:.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;
    

【问题讨论】:

AFAIK 将模板声明与定义分开被认为是不好的做法。至少不要使用 .cpp,使用 .tpp。充其量,在您声明它们的地方定义它们。这对你和编译器来说都少了很多工作。 【参考方案1】:

在我看来,您的困惑源于不知道 #pragma once 和翻译单元是如何工作的。

#pragma once,很像包含守卫,防止文件的内容(通常是标题)被多次拉入单个翻译单元。

如果你#include &lt;vector&gt;在多个实现文件中,内容将全部拉入,但每个翻译单元只提取一次。

所以你应该删除#include "myVector.cpp",因为MSVS会自动编译实现文件,这也是错误的。

请注意,模板定义必须是可见的,因此您需要将它们移动到 .h 文件中,就像您所做的那样,或者使用您当前的方法,将 .cpp 文件重命名为 @987654327 之类的名称@ 甚至 .h 并包含它。

【讨论】:

我很困惑。我目前使用的是#include "myVector.h" 而不是#include &lt;vector&gt; 如果我删除#include "myVector.cpp",那么我会收到链接器错误(请参阅对其他答案的评论)。文件的扩展名对VS很重要吗?如果我保留 .h 中的包含,则将文件名更改为 myVector.impl 有效。 @soandos 是的,扩展很重要。 #include &lt;vector&gt; 只是一个例子。

以上是关于VS 2011 模板类的主要内容,如果未能解决你的问题,请参考以下文章

❥关于C++之模板类vectorarray VS 数组

vs2010自定义项目模板

使用模板时缺少默认构造函数

在VS中,每次添加Class类都需要加上Public(教你怎么样设置类模板头部)

如何区分模板类中存储的类型

C++类模板:不是类型名称、静态或枚举数