c++ - 在 C++ 模板代码中找不到构造函数

标签 c++ oop templates constructor

<分区>

使用 g++ main.cpp Vec.cpp -Wall -o main -I 编译时出现此错误。

/tmp/cciqbEQJ.o: In function `main':
main.cpp:(.text+0x8b): undefined reference to `Vec<double>::Vec()'
main.cpp:(.text+0x9b): undefined reference to `Vec<double>::~Vec()'
collect2: ld returned 1 exit status
make: *** [all] Error 1

我不明白,因为我之前做过很多多源文件程序,从来没有出现过找不到构造函数的错误。编译器似乎无法将模板代码动态绑定(bind)到模板的实例化。另外,我在 .h 文件上放置了一个宏守卫,但它没有显示在下面。

源代码如下:

Vec.cpp

#include "Vec.h"

using namespace std;

template<class T>
Vec<T>::Vec() {
   create();
}


template<class T>
Vec<T>::Vec( size_type n, const value_type& t ){
        create(n,t);
}
template<class T>
Vec<T>::Vec(const Vec& v)
{
        create(v.begin(), v.end());
}

template<class T>
Vec<T>::~Vec(){
    uncreate();
}

   template<class T>
   void Vec<T>::create()
{
 data = avail = limit = 0;
}

   template<class T>
   void Vec<T>::create(size_type n, const T& val)
{
  data = alloc.allocate(n);
  limit = avail = data + n;
  uninitialized_fill(data,limit, val);
}

template<class T>
void Vec<T>::create(const_iterator i, const_iterator j) {

    data = alloc.allocate(j-i);
    limit = avail = uninitialized_copy(i, j, data);
}
    template<class T> 
    void Vec<T>::uncreate() {

            if (data) {

                    iterator it = avail;
                    while (it != data)
                            alloc.destroy(--it);

                    alloc.deallocate(data,limit-data);
            }
            data = limit = avail =0;
    }

    template<class T> void Vec<T>::grow() {
            size_type new_size = max ( 2 * (limit-data), ptrdiff_t(1));

            iterator new_data = alloc.allocate(new_size);
            iterator new_avail = unitialized_copy(data, avail, new_data);

            uncreate();
            data = new_data;
            avail = new_avail;
            limit = data + new_size;

    }


    template<class T> void Vec<T>::unchecked_append(const T& val) {
            alloc.construct(avail++, val);
    }

    template<class T>
    void Vec<T>::push_back(const T& t){
                    if ( avail == limit )
                            grow();

                    unchecked_append(t);
    }

Vec.h

    template<class T> class Vec{
    public:
            typedef T* iterator;
            typedef const T* const_iterator;
            typedef size_t size_type;
            typedef T value_type;

            Vec();
            Vec( size_type n, const T& t=T() );

            Vec(const Vec& v);
            Vec& operator=(const Vec& v);

            ~Vec();
            void push_back(const T& t);

            inline size_type size() const { return limit - data; }

            inline iterator begin() {return data;}
            inline const_iterator begin() const { return data; }

            inline iterator end() { return limit; }
            inline const_iterator end() const { return limit; }

            inline T& operator[](size_type i){
                    return data[i];
            }
            const T& operator[](size_type i) const { return data[i]; }


    private:
            iterator data;
            iterator limit;
            iterator avail;

            //facilities for memory allocation
            allocator<T> alloc;

            //allocate and initialize the underlying array
            void create();
            void create(size_type, const T&);
            void create(const_iterator, const_iterator);

            //destroy the elements in the array and free the memory
            void uncreate();

            //support functions for push_back
        void grow();
        void unchecked_append(const T&);
};

主要.cpp

 int main(void) {
   Vec<double> test;
 }             

最佳答案

为了让编译器生成代码,它必须看到模板定义和用于模板的特定类型。

因此,在 main.cpp 中,仅在 顶部 添加行 #include "Vec.cpp"

编译使用 g++ main.cpp -Wall -o main -I. <-- 注意 Vec.cpp 现在已删除。

这将确保模板声明和实现在编译期间在一起,同时实现仍然与声明分开。

另一种方法是在 Vec.hend 中包含此 .cpp,如上述 SO 的评论链接中所建议的胡安乔潘萨

有关更多详细信息,请引用:- Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file ?

关于c++ - 在 C++ 模板代码中找不到构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17951827/

相关文章:

c++ - 同时使用 const 和 non-const 进行参数传递

链表的C++继承

java - 更新类的Jpanel

c++ - 为 std::to_string() 获取自己的补丁,以便在旧编译器上与 double 一起正常工作

c++ - 在具有大型代码库的 Eclipse IDE 中搜索函数或文本

c++ - 谷歌测试 C++ : Is there a way to read the current console output in a test?

model-view-controller - 做MVC还是不做?

c++ - C++ 模板中的段错误

c++ - 使用 stringstream 将字符串转换为数字

c++ - 将 ifstream 转换为 istream