链接有问题

发布于 2024-12-06 14:43:04 字数 1494 浏览 0 评论 0原文

我有 3 个 C++ 文件: genericStack.h:

template <class T> 
class Stack{
 public:
  Stack (int size){
    top = -1;
    MAX_SIZE = size;
    v = new T (size);
  }
  ~Stack(){ delete v;}

  T pop();
  void push (T);

  class Underflow{};
  class Overflow{};

  private:
   int top;
   int MAX_SIZE;
   T* v;
 };

genericStackImpl.c++:

#include "genericStack.h"

template <class T>
void Stack <T> :: push (T c){ 
  if (top == MAX_SIZE - 1) throw Overflow();
  v[++top] = c;
} 

template <class T>
T Stack <T> :: pop(){
  if (top < 0) throw Underflow();
  return v[top--];
}

driver.c++:

#include <iostream>
#include "genericStack.h"
int main(){
 Stack<char> sc(3);
 try{
   while (true) sc.push ('p');
 }
 catch (Stack<char>::Overflow){std::cout << "Overflow caught\n";}
 try{
  while (true) std::cout << sc.pop() << '\n';
 }
 catch (Stack<char>::Underflow){ std::cout << "Underflow caught\n";}
 return 0;
}

当我使用 g++ 4.5 编译时:

g++ -o driver driver.c++ genericStackImpl.c++

我收到这些错误:

/tmp/ccLXRXgF.o: In function `main':
driver.c++:(.text+0x2e): undefined reference to `Stack<char>::push(char)'
driver.c++:(.text+0x3c): undefined reference to `Stack<char>::pop()'
collect2: ld returned 1 exit status

我不明白问题是什么。如果我在驱动程序文件中移动实现,那么它就会编译并运行。

I have 3 C++ files:
genericStack.h:

template <class T> 
class Stack{
 public:
  Stack (int size){
    top = -1;
    MAX_SIZE = size;
    v = new T (size);
  }
  ~Stack(){ delete v;}

  T pop();
  void push (T);

  class Underflow{};
  class Overflow{};

  private:
   int top;
   int MAX_SIZE;
   T* v;
 };

genericStackImpl.c++:

#include "genericStack.h"

template <class T>
void Stack <T> :: push (T c){ 
  if (top == MAX_SIZE - 1) throw Overflow();
  v[++top] = c;
} 

template <class T>
T Stack <T> :: pop(){
  if (top < 0) throw Underflow();
  return v[top--];
}

driver.c++:

#include <iostream>
#include "genericStack.h"
int main(){
 Stack<char> sc(3);
 try{
   while (true) sc.push ('p');
 }
 catch (Stack<char>::Overflow){std::cout << "Overflow caught\n";}
 try{
  while (true) std::cout << sc.pop() << '\n';
 }
 catch (Stack<char>::Underflow){ std::cout << "Underflow caught\n";}
 return 0;
}

When i compile using g++ 4.5:

g++ -o driver driver.c++ genericStackImpl.c++

I get these errors:

/tmp/ccLXRXgF.o: In function `main':
driver.c++:(.text+0x2e): undefined reference to `Stack<char>::push(char)'
driver.c++:(.text+0x3c): undefined reference to `Stack<char>::pop()'
collect2: ld returned 1 exit status

I dont understand what the problem is. If i move the implementation in the driver file, then it compiles and runs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-12-13 14:43:04

一般来说,模板定义也需要在头文件中。一个例外是当您显式实例化或使用显式专业化时,您两者都没有做。

解决此问题的一种方法是将 genericStackImpl.c++ 的内容移至其头文件的底部。

这样做的原因是因为模板函数不是实际的函数,它们只是模板。模板用于(实例化)创建实际函数,这些就是您链接的内容。

genericStackImpl.c++ 中没有函数。这些函数只有在您使用它们时才会创建,即编译器第一次看到 sc.pushsc.pop 时。不幸的是,当driver.c++尝试创建这些函数时,它无法找到模板主体——它们隐藏在genericStackImpl.c++中!相反,它只是编译对这些函数的引用,希望其他文件能够生成它们。

最后,当谈到链接时,链接器无法在任何地方找到该函数,因此它会给出错误。

解决此问题的另一种方法是在 genericStackImpl.c++ 中自己显式实例化这些函数,即

template class Stack<char>;

这将创建函数,链接器将找到它们。

这种方法的问题在于,它要求您事先知道要在堆栈中使用什么类型,因此大多数人只是将模板定义放在头文件中。

Generally speaking, template definitions need to also be in the header file. An exception to this is when you are explicitly instantiating, or using explicit specialisations, neither of which you are doing.

One way to solve this would be to move the contents of genericStackImpl.c++ to the bottom of its header file.

The reason for this is because template functions are not actual functions, they are just templates. A template is used (instantiated) to create actual functions, and those are what you link against.

There are no functions in genericStackImpl.c++. The functions only get created once you use them, i.e. the first time the compiler sees sc.push and sc.pop. Unfortunately, when driver.c++ tries to create these functions, it can't find the template bodies -- they hidden in genericStackImpl.c++! Instead, it just compiles a reference to those functions, hoping that some other file will make them.

Finally, when it comes to link time, the linker can't find the function anywhere, so it gives you an error.

Another way to solve this would be to explicitly instantiate those functions yourself in genericStackImpl.c++, i.e.

template class Stack<char>;

This will create the functions, and the linker will find them.

The problem with this approach is that it require you to know what types your going to be using in your stack beforehand, so most people just put template definitions in the header file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文