【问题标题】:C++ Undefined reference to destructor and reference to local variable returnedC++ 未定义的对析构函数的引用和对返回的局部变量的引用
【发布时间】: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


【解决方案1】:
  1. 您的某些函数(例如 operator -())正在返回 引用 到堆栈分配的变量。一旦函数退出,这些将超出范围。

这是未定义的行为

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

  1. 您需要为~Vect()~Vect_fixe() 提供一个实现。如果不需要它们,请不要在类中声明它们。

【讨论】:

  • 感谢您的快速答复!我想通过引用返回类型意味着我正在返回我创建的临时变量?我了解,但在这种情况下,您会建议直接修改对象吗?我没有看到其他返回修改版本的方法。
  • 确实,您不能通过引用返回临时变量。如果您可以返回修改后的this,那么返回*this 作为参考就可以了。
【解决方案2】:

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

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

【讨论】:

  • 哦,我认为纯虚拟析构函数不需要那个。我的错,我对继承还是很陌生。非常感谢:)
  • 是的,除非您指定 virtual ~Vect() = default,否则他们需要这样做
  • 向量是一个使用继承的相当奇怪的类,但它仍然是一个很好的做法。
  • 希望有一天能派上用场~
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多