【发布时间】:2015-11-26 16:08:17
【问题描述】:
我正在尝试创建一个从另一个类模板继承的类模板,第一个是一个只包含纯虚方法的类,第二个是要被操作的类。我要构建的是一个矢量类,其大小由第二个模板参数确定。基类将用于创建另一个向量类,该类的每个对象的大小都不同。
折腾了2-3天,想不通怎么解决编译项目时出现的一系列错误。
这是标题(Vect.hpp)
#ifndef NEWFILE1_HPP
#define NEWFILE1_HPP
#include <cstddef>
#include <iostream>
#include <exception>
#include <stdexcept>
template <class elem>
class Vect
{
public:
virtual elem& operator[] (std::ptrdiff_t nIndex) =0;
virtual const elem& operator[] (std::ptrdiff_t nIndex) const =0;
virtual Vect& operator+() =0;
virtual Vect& operator-() =0;
virtual Vect& operator+(const elem&) =0;
virtual Vect& operator-(const elem&) =0;
//virtual Vect& operator*(const elem&) =0;
virtual ~Vect();
};
template <class elem, std::size_t taille=10>
class Vect_fixe: public Vect<elem>
{
template <typename T, std::size_t D>
friend std::ostream& operator<< (std::ostream&, const Vect_fixe<T, D>&);
public:
Vect_fixe():vecteur(new elem[taille]) {};
virtual elem& operator[] (std::ptrdiff_t nIndex);
virtual const elem& operator[] (std::ptrdiff_t nIndex) const;
virtual Vect_fixe& operator+();
virtual Vect_fixe& operator-();
virtual Vect_fixe& operator+(const elem&);
virtual Vect_fixe& operator-(const elem&);
private:
elem vecteur[taille];
~Vect_fixe(){delete[] vecteur;}
};
这是定义函数并进行测试的那个(main.cpp)
#include <cstdlib>
#include "Vect.hpp"
#include <exception>
#include <stdexcept>
template <class elem, std::size_t taille>
elem& Vect_fixe<elem, taille>::operator[] (std::ptrdiff_t nIndex)
{
if (nIndex >= taille)
throw std::out_of_range("Index out of range.");
return vecteur[nIndex];
};
template <class elem, std::size_t taille>
const elem& Vect_fixe<elem, taille>::operator[] (std::ptrdiff_t nIndex) const
{
if (nIndex >= taille)
throw std::out_of_range("Index out of range.");
return vecteur[nIndex];
};
template <class elem, std::size_t taille>
Vect_fixe<elem, taille>& Vect_fixe<elem, taille>::operator +()
{
return *this;
}
template <class elem, std::size_t taille>
Vect_fixe<elem, taille>& Vect_fixe<elem, taille>::operator -()
{
Vect_fixe<elem, taille> temp_v;
for (int i=0; i<taille; i++)
temp_v[i] = -temp_v[i];
return temp_v;
}
template <class elem, std::size_t taille>
Vect_fixe<elem,taille>& Vect_fixe<elem, taille>::operator+(const elem&)
{
Vect_fixe<elem, taille> temp_v;
elem obj;
for (int i=0; i<taille; i++)
temp_v[i] += obj;
return temp_v;
}
template <class elem, std::size_t taille>
Vect_fixe<elem,taille>& Vect_fixe<elem, taille>::operator-(const elem&)
{
Vect_fixe<elem, taille> temp_v;
elem obj;
for (int i=0; i<taille; i++)
temp_v[i] -= obj;
return temp_v;
}
int main() {
int x;
x = 5;
Vect_fixe<int, 10> vect;
//std::cout<< vect << std::endl;
return 0;
}
我得到的错误如下:
g++ -std=c++14 -o dist/Debug/Cygwin_4.x-Windows/cppapplication_2 build/Debug/Cygwin_4.x-Windows/Vect.o build/Debug/Cygwin_4.x-Windows/main.o
build/Debug/Cygwin_4.x-Windows/main.o: In function `Vect_fixe<int, 10ul>::~Vect_fixe()':
/cygdrive/e/University/BA2/Langages de Programmation/Projet C++/CppApplication_2/Vect.hpp:48: undefined reference to `Vect<int>::~Vect()'
/cygdrive/e/University/BA2/Langages de Programmation/Projet C++/CppApplication_2/Vect.hpp:48:(.text$_ZN9Vect_fixeIiLm10EED1Ev[_ZN9Vect_fixeIiLm10EED1Ev]+0x3f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Vect<int>::~Vect()'
collect2: error: ld returned 1 exit status
导致这种状态的原因是我试图修复以前的错误,即返回对局部变量的引用。我将 const 关键字放在一些函数中,但这似乎并没有解决问题,所以我恢复了,这就是发生的事情。这个错误对我来说真的很不清楚,可能是因为我习惯于用 Python 编程,几乎每个错误都有解释。
【问题讨论】:
-
你应该坚持每个帖子一个问题。
-
@juanchopanza 从技术上讲,只有一个问题,我只是给出了它发生的背景。但是,是的,也许我选择的标题不是最好的。
-
您的错误不是编译器错误,而是链接器错误。你基本上是在制造
g++ -std=c++14 -o Vect.o main.o,这是无稽之谈。缺少可执行文件名,在这里您将 main.o 链接到 Vect.o。您的链接命令应类似于g++ -o myApp Vect.o main.o。如果是Makefile生成链,请检查;如果是 IDE,则必须仔细检查构建链。 -
你的 Vect 析构函数需要一个身体。
-
技术上,这里没有问题。但您似乎在问两个不同的问题。
标签: c++ templates inheritance compiler-errors undefined-reference