C++ 未定义的对析构函数的引用和对返回的局部变量的引用
C++ Undefined reference to destructor and reference to local variable returned
我正在尝试创建一个继承自另一个 class 模板的 class 模板,第一个是 class 只包含纯虚拟方法,第二个是那将被操纵。我要构建的是一个矢量 class ,其大小由第二个模板参数决定。基础 class 将用于创建另一个向量 class,其大小对于 class 的每个 object 都是不同的。
折腾了2-3天,编译项目时出现的一系列错误,一直想不出如何解决。
这里是 header (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
导致这种状态的原因是我试图修复以前的错误,即 return 对局部变量的引用。我在一些函数中加入了 const 关键字,但这似乎并没有解决问题,所以我恢复了,这就是发生的事情。这个错误对我来说真的不清楚,可能是因为我习惯了 Python 中几乎每个错误都得到解释的编程。
- 您的某些函数(例如
operator -()
)正在 returning 引用 到堆栈分配的变量。一旦函数退出,这些将超出范围。
那是未定义的行为。
补救措施是return 值副本。从函数中删除引用 return 类型。
- 您需要为
~Vect()
和 ~Vect_fixe()
提供实现。如果不需要它们,则不要在 class. 中声明它们
您忘记为 Vect
实现析构函数,您应该在您的 cpp 文件中为 Vect_fixe
实现析构函数。这将删除您对析构函数的未定义引用。
如果您将 operator-
的 return 类型从 Vect&
更改为 Vect
,您的错误 reference to a local variable
应该得到修复。您不能 return 对局部变量的引用。那是未定义的行为。
我正在尝试创建一个继承自另一个 class 模板的 class 模板,第一个是 class 只包含纯虚拟方法,第二个是那将被操纵。我要构建的是一个矢量 class ,其大小由第二个模板参数决定。基础 class 将用于创建另一个向量 class,其大小对于 class 的每个 object 都是不同的。
折腾了2-3天,编译项目时出现的一系列错误,一直想不出如何解决。
这里是 header (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
导致这种状态的原因是我试图修复以前的错误,即 return 对局部变量的引用。我在一些函数中加入了 const 关键字,但这似乎并没有解决问题,所以我恢复了,这就是发生的事情。这个错误对我来说真的不清楚,可能是因为我习惯了 Python 中几乎每个错误都得到解释的编程。
- 您的某些函数(例如
operator -()
)正在 returning 引用 到堆栈分配的变量。一旦函数退出,这些将超出范围。
那是未定义的行为。
补救措施是return 值副本。从函数中删除引用 return 类型。
- 您需要为
~Vect()
和~Vect_fixe()
提供实现。如果不需要它们,则不要在 class. 中声明它们
您忘记为 Vect
实现析构函数,您应该在您的 cpp 文件中为 Vect_fixe
实现析构函数。这将删除您对析构函数的未定义引用。
如果您将 operator-
的 return 类型从 Vect&
更改为 Vect
,您的错误 reference to a local variable
应该得到修复。您不能 return 对局部变量的引用。那是未定义的行为。