C++返回了对析构函数的未定义引用和对局部变量的引用
C++ Undefined reference to destructor and reference to local variable returned
我正在尝试创建一个从另一个类模板继承的类模板,第一个是一个只包含纯虚拟方法的类,第二个是将要操作的类。我试图构建的是一个向量类,其大小由第二个模板参数决定。基类将用于创建另一个向量类,该向量类的每个对象的大小都不同。
在折腾了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编程,几乎每个错误都会得到解释。
- 您的一些函数(例如
operator -()
)正在返回对堆栈分配变量的引用。一旦函数退出,这些将超出范围
这是未定义的行为。
补救措施是返回一个值副本。从函数中删除引用返回类型。
- 您需要提供
~Vect()
和~Vect_fixe()
的实现。如果不需要它们,那么就不要在类中声明它们
您忘记为Vect
实现析构函数,而应该在cpp文件中为Vect_fixe
实现析构因子。这将删除对析构函数的未定义引用。
如果将operator-
的返回类型从Vect&
更改为Vect
,则应修复错误reference to a local variable
。不能返回对局部变量的引用。这是未定义的行为。
相关文章:
- 将引用分配给局部变量,如果局部变量超出范围,它会超出范围吗?
- 返回对局部变量 - C++ 的引用
- 警告 C4101 未引用的局部变量
- 现代编译器会优化只引用对象子集的局部变量吗
- 右值引用和局部变量的移动
- 结构数组 - 未引用的局部变量
- 返回对局部变量的引用是否正常工作?
- 为什么 ++ 运算符触发器返回对局部变量警告的引用
- 三元运算符:编译器不发出局部变量警告的返回引用
- 为什么 C++ 编译器不警告返回对局部变量的引用
- 变量循环范围会导致返回局部变量的地址引用
- 通过引用返回局部变量
- 为什么我得到未引用的局部变量
- 返回由引用指定的引用局部变量
- 返回静态局部变量作为引用
- 从函数中引用局部变量不会给出错误
- C++按引用局部变量传递
- 为什么可以通过引用局部变量而不是临时变量来返回函数?c++
- 捕获带有未引用局部变量警告的异常
- c++11时间非引用局部变量