C++返回了对析构函数的未定义引用和对局部变量的引用

C++ Undefined reference to destructor and reference to local variable returned

本文关键字:引用 局部变量 未定义 析构函数 返回 C++      更新时间:2023-10-16

我正在尝试创建一个从另一个类模板继承的类模板,第一个是一个只包含纯虚拟方法的类,第二个是将要操作的类。我试图构建的是一个向量类,其大小由第二个模板参数决定。基类将用于创建另一个向量类,该向量类的每个对象的大小都不同。

在折腾了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编程,几乎每个错误都会得到解释。

  1. 您的一些函数(例如operator -())正在返回对堆栈分配变量的引用。一旦函数退出,这些将超出范围

这是未定义的行为。

补救措施是返回一个值副本。从函数中删除引用返回类型。

  1. 您需要提供~Vect()~Vect_fixe()的实现。如果不需要它们,那么就不要在类中声明它们

您忘记为Vect实现析构函数,而应该在cpp文件中为Vect_fixe实现析构因子。这将删除对析构函数的未定义引用。

如果将operator-的返回类型从Vect&更改为Vect,则应修复错误reference to a local variable。不能返回对局部变量的引用。这是未定义的行为。