在 C++ VSCode 中修复错误“对“Class::Function”的未定义引用

问题描述

我对 C++ 和整个编程还很陌生,所以我从来没有遇到过这样的问题,并且已经被困了几个小时而不知道如何解决它。

我正在尝试为大学项目自己制作一个矢量类,因为我们无法使用 <vector> 库。

这是我的代码

Vector.h

#pragma once
#include <iostream>

template <typename T>
class Vector {
private:
   T* data;
   size_t size;
   size_t capacity;

   void resize();

public:
   Vector();
   Vector(const Vector<T>& other);
   Vector& operator=(const Vector<T>& other);
   ~Vector();

   void push_back(T element);
   void pop_back();
   void push_front(T element);
   
   bool is_empty() const;
   void print() const;

   T& operator[](size_t index);
   const T& operator[](size_t index) const;

};

Vector.cpp

#include "Vector.h"

template <typename T>
void Vector<T>::resize() {
   this->capacity *= 2;
   T* temp = new T[this->capacity];

   for(size_t i = 0; i < this->size; ++i) {
      temp[i] = this->data[i];
   }
   delete[] this->data;
   this->data = temp;
}

template <typename T>
Vector<T>::Vector() {
   this->size = 0;
   this->capacity = 0;
   this->data = new T[this->capacity];
}

template <typename T>
Vector<T>::Vector(const Vector<T>& other) {
   this->capacity = other.capacity;
   this->size = other.size;
   this->data = new T[other.capacity];
   for(size_t i = 0; i < this->size; ++i) {
      this->data[i] = other.data[i];
   }
}

template <typename T>
Vector<T>& Vector<T>::operator=(const Vector<T>& other) {
   if(this != &other) {
      delete[] this->data;

      this->capacity = other.capacity;
      this->size = other.size;
      this->data = new T[other.capacity];
      for(size_t i = 0; i < this->size; ++i) {
         this->data[i] = other.data[i];
      }
   }

   return *this;
}

template <typename T>
Vector<T>::~Vector() {
   delete[] this->data;
}

template <typename T>
void Vector<T>::push_back(T element) {
   this->size++;
   if(this->size >= this->capacity) {
      this->resize();
   }

   this->data[this->size - 1] = element;
}

template <typename T>
void Vector<T>::pop_back() {
   if(this->size == 0) {
      std::cerr << "The vector is empty" << std::endl;
      return;
   }

   T* new_array = new T[this->size - 1];
   for(size_t i = 0; i < this->size - 1; ++i) {
      new_array[i] = this->data[i];
   }

   delete[] this->data;
   --this->size;
   this->data = new_array;
}

template <typename T>
void Vector<T>::push_front(T element) {
   this->size++;
   if(this->size >= this->capacity) {
      this->resize();
   }

   for(size_t i = this->size - 1; i > 0; --i) {
      this->data[i] = this->data[i - 1];
   }
   this->data[0] = element;
}

template <typename T>
bool Vector<T>::is_empty() const {
   if(this->size == 0) {
      return true;
   }
   else {
      return false;
   }
}

template <typename T>
void Vector<T>::print() const {
   for(size_t i = 0; i < this->size; ++i) {
      std::cout << this->data[i] << ",";
   }
}

template <typename T>
T& Vector<T>::operator[](size_t index) {
   return this->data[index];
}

template <typename T>
const T& Vector<T>::operator[](size_t index) const {
   return this->data[index];
}

ma​​in.cpp

#include "Vector.h"

int main () {
   Vector<int> vec;
   vec.push_back(1);
   vec.push_back(2);
   vec.push_back(3);
   vec.push_back(4);
   vec.push_front(0);
   vec.pop_back();
   vec.print();
}

我正在尝试运行它来测试它是如何工作的,但我收到了这些错误

PS C:\projects\my-project> cd "c:\projects\my-project\"; if ($?) { g++ main.cpp -o main } ; if ($?) { .\main }
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x1b): undefined reference to `Vector<int>::Vector()'       
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x2c): undefined reference to `Vector<int>::push_back(int)' 
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x3d): undefined reference to `Vector<int>::push_back(int)' 
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x4e): undefined reference to `Vector<int>::push_back(int)' 
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x5f): undefined reference to `Vector<int>::push_back(int)' 
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x70): undefined reference to `Vector<int>::push_front(int)'
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x7c): undefined reference to `Vector<int>::pop_back()'   
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x88): undefined reference to `Vector<int>::print() const'
C:\Users\Name\AppData\Local\Temp\ccE3Cxfs.o:main.cpp:(.text+0x94): undefined reference to `Vector<int>::~Vector()'    
collect2.exe: error: ld returned 1 exit status

在这里和其他论坛上阅读了很多文章,但似乎没有任何帮助。我尝试通过将 tasks.json 更改为 ${file} 来修复 ${workspaceFolder}/*.cpp 文件,因为它建议here,尝试在终端中手动输入文件名称,就像它所说的{ {3}} 和 here 但没有解决问题。

使其工作的唯一方法是在 Vector.cpp 中包含 Vector.h 而不是 main.cpp,但根据我所阅读的和被告知的内容包括 .cpp 文件而不是.h 文件总是不好的做法,尤其是在大型项目中。

我可能遗漏了一些明显的东西,但如果有人能帮助我,我将不胜感激

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)