C++的类模板的问题(实现文件和头文件放在一起), 因为模板不支持分离编译

首先C++的模板不支持分离编译模型。如果不是模板, 那么实现文件和接口文件分开会是一个good practice in C++。 但是对于模板,  我们必须要将实现文件 和  头文件放在一起。 否则会发生undefined refernce to .....

例如下例中, 我们修改为如下, 一旦将实现文件和头文件分开, 会错的很难看的。



具体的, 各个文件如下:

LinkedListNode.h 文件如下:

#ifndef LINKED_LIST_NODE_H
#define LINKED_LIST_NODE_H
//definition of the node
template <class Type>
struct nodeType {
   Type info;
   nodeType<Type>* link;
};
#endif


LinkedListIterator.h:

#ifndef LINKED_ITERATOR
#define LINKED_ITERATOR
//This class specifies the members to implement an iterator
#include "LinkedListNode.h"
template <class Type>
class linkedListIterator {
   public:
      linkedListIterator();
      //default constructor
      //postcondition: current = NULL;

      linkedListIterator(nodeType<Type>* ptr);
      //constructors with a parameter
      //postcondition: current = ptr;

      Type operator*();
      //function to overload the dereferencing operator *
      //postcondition: returns the info contained in the node.

      linkedListIterator<Type> operator++();
      //overload the preincrement operator
      //postcondition: the iterator is advanced to the next node.

      bool operator==(const linkedListIterator<Type>& right) const;
      //overload the equality operator
      //postcondition: returns true if the iterator is equal to the
      //iterator specifies by right, otherwise it returns false.

      bool operator!=(const linkedListIterator<Type>& right) const;
      // overload the not equal to operator.
      //postcondition: returns true if this iterator is not equal
      //to the iterator specifies by right, otherwise it returns false.
   private:
      nodeType<Type>* current; //pointer to point to the current node in the linked list
};

template <class Type>
linkedListIterator<Type>::linkedListIterator() {
   current = NULL;
}

template <class Type>
linkedListIterator<Type>::linkedListIterator(nodeType<Type>* ptr) {
   current = ptr;
}

template <class Type>
Type linkedListIterator<Type>::operator*() {
   return current -> info;
}

template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++() {
   current = current -> link;
   return *this;
}

template <class Type>
bool linkedListIterator<Type>::operator==(const linkedListIterator<Type>& right) const {
    return (right == right.current);
}

template <class Type>
bool linkedListIterator<Type>::operator!=(const linkedListIterator<Type> &right) const {
   return (current != right.current);
}


#endif


LinkedListType.h:

#ifndef LINKED_TYPE_H
#define LINKED_TYPE_H
//this class specifies members to implement the basic properties
//of a linked list. This is an abstract class. We cannot instantiate
//an object of this class

#include "linkedListIterator.h"
#include "LinkedListNo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们需要将模板类的定义和实现分离到不同的文件中时,可以将模板类的声明放在一个头文件中,将模板类的实现放在一个cpp文件中。 假设我们有一个模板类`MyClass`的声明: ```c++ // MyClass.h #pragma once template<typename T> class MyClass { public: MyClass(T value); void printValue(); private: T m_value; }; ``` 在模板类的定义中,我们只需要声明构造函数和`printValue`函数的方法,而不需要提供函数的具体实现,因为这些实现将在另一个文件中提供。 现在我们需要在一个cpp文件中提供模板类的实现: ```c++ // MyClass.cpp #include "MyClass.h" #include <iostream> template<typename T> MyClass<T>::MyClass(T value) : m_value(value) {} template<typename T> void MyClass<T>::printValue() { std::cout << "Value: " << m_value << std::endl; } // 显式实例化模板类 template class MyClass<int>; template class MyClass<float>; ``` 在这个文件中,我们提供了模板类的实现,包括构造函数和`printValue`函数的具体代码。此外,我们还需要显式实例化模板类,这将在编译时生成模板类的实例化代码。在这个例子中,我们实例化了`MyClass<int>`和`MyClass<float>`两个类型的模板类。 最后,在需要使用模板类的文件中,只需要包含头文件即可: ```c++ // main.cpp #include "MyClass.h" int main() { MyClass<int> obj(42); obj.printValue(); return 0; } ``` 在这个例子中,我们实例化了一个`MyClass<int>`类型的对象,并调用了它的`printValue`函数。编译器将在编译时生成实例化的代码,并将其链接到最终的可执行文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值