C++ 重载运算符声明和定义问题

发布于 2024-07-23 04:57:05 字数 499 浏览 6 评论 0原文

我很难让它工作,

file: myclass.hpp

Class MyClass {
public:
  template <class T>    
  MyClass &operator<<(const T &val);
};


file: myclass.cpp

template <class T>
MyClass &MyClass::operator<<(const T &val) {
   ...  
}

我可以毫无问题地将其编译为对象,但是当其他函数尝试调用它时,会出现此错误(每次使用 << 时)。

myclass.cpp: undefined reference to `MyClass& MyClass::operator<< <int>(int const&)'

我究竟做错了什么?

I am having a hard time getting this to work

file: myclass.hpp

Class MyClass {
public:
  template <class T>    
  MyClass &operator<<(const T &val);
};


file: myclass.cpp

template <class T>
MyClass &MyClass::operator<<(const T &val) {
   ...  
}

I can compile this in to a object without a problem, But when other functions try to calling it, this error comes up (for every time << was used).

myclass.cpp: undefined reference to `MyClass& MyClass::operator<< <int>(int const&)'

What am I doing wrong?

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

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

发布评论

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

评论(3

洒一地阳光 2024-07-30 04:57:05

如果您想在单独的编译单元中定义模板的实例(通常是这种情况),那么您不能在单独的 cpp 中定义模板方法。 在编译使用该模板类的编译单元时,每个模板方法都必须对编译器可见。 因此,跨cpps使用模板时,必须在标头中定义模板。 模板实际上是生成类的方法,而不是生成类本身的方法。 因此,当编译器看到

YourClass<int>

它需要在编译时查看整个 YourClass 模板以生成名为的类型

YourClass<int>

及其所有方法,这些方法与 say 完全独立的方法

YourClass<float>

这意味着它必须查看所有 C++ 源代码。 如果模板的这两种用途在单独的 cpp 中实例化,那么编译器生成这两种用途的唯一方法就是在一个标头中完全定义模板。

查看我的答案此处了解更多信息。

感谢您的有用评论,极大地改进了这个答案

If you want to define instances of your template in separate compilation units (which is typically the case) then you can't define template methods in a separate cpp. Every template method must be visible to the compiler when compiling compilation units that use that template class. Therefore when using the template across cpps, the template must be defined in a header. Template's are really ways of generating classes not classes in-and-of-themselves. Therefore when the compiler sees

YourClass<int>

it needs to see the entire YourClass template at compile time to generate a type called

YourClass<int>

and all its methods, which have completely separate methods from say

YourClass<float>

Which means it must see all the C++ source code. If these two uses of the template are instantiated in separate cpp's then the only way only way the compiler can generate both is to fully define the template in one header.

See my answer here for more info.

Thanks for the useful comments that greatly improved this answer

梦幻的味道 2024-07-30 04:57:05

将运算符的定义放在 .hpp 中,而不是 .cpp 中。 当为某些新类型实例化模板时,编译器需要能够看到模板的完整定义,以便它可以生成此专门化的代码。

C++ 常见问题解答精简版

Put the definition of the operator in the .hpp instead of the .cpp. The compiler needs to be able to see the complete definition of the template when it gets instantiated for some new type, so that it can generate the code for this specialization.

There are also some questions and answers about this kind of template related linker errors and different possible solutions in the C++ FAQ Lite.

一片旧的回忆 2024-07-30 04:57:05

模板需要在头文件中定义,而不是在cpp文件中。 实际的实现是在使用时根据需要在每个编译单元中创建的。 在这方面它们有点像宏。

Templates need to be defined in the header file rather than the cpp file. The actual implementations are created in each compilation unit on demand as they're used. They're kind of like macros in that respect.

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