Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 首次尝试正确的模块化编程时遇到的问题_C++ - Fatal编程技术网

C++ 首次尝试正确的模块化编程时遇到的问题

C++ 首次尝试正确的模块化编程时遇到的问题,c++,C++,我第一次尝试模块化编程时遇到了一个问题 我想知道是否有人能就如何解决这个问题提出一个可能的方向 这是我的代码,也是拉兹洛的《计算几何》一书中的代码。我做的唯一不同的事情就是把它分成更小的部分和一个单位头文件 代码 标题.h #ifndef NULL #define NULL 0 #endif #ifndef A_H #define A_H // -----Definition of Node Class ---------------------------------- class Nod

我第一次尝试模块化编程时遇到了一个问题

我想知道是否有人能就如何解决这个问题提出一个可能的方向

这是我的代码,也是拉兹洛的《计算几何》一书中的代码。我做的唯一不同的事情就是把它分成更小的部分和一个单位头文件

代码

标题.h

#ifndef NULL
#define NULL 0
#endif
#ifndef A_H
#define A_H

// -----Definition of Node Class ----------------------------------

class Node{

    protected:
        Node *_prev;
        Node * _next;
        static int cindex;


    public:
        int index;
        Node(void);
        virtual ~Node(void);
        Node *next(void); // accessor
        Node *prev(void); //accessor
        Node *insert(Node*);
        Node*remove(void);
        void splice (Node*);

    };

// ------ end of definition of Node --------------------
//======================================================
//----------Start of Definition of ListNode class ------

template < class T > class List;


template<class T> class ListNode: public Node {

    public:
        T _val;
        ListNode(T val);
        friend class List<T>;


    };

// ------ End of Definition of ListNode class --------------------
//====================================================++
//----------Start of Definition of List class ------

template<class T> class List {

    private:
        ListNode<T> *header;
        ListNode<T> *win;
        int _length;
    public:
        List(void);
        ~List(void);
        T insert(T);
        T append(T);
        T prepend(T);
        List * append(List*);
        T remove(void);
        void val(T);
        T val(void);
        T next(void);
        T prev(void);
        T first(void);
        T last(void);
        int length(void);
        bool isFirst(void);
        bool isLast(void);
        bool isHead(void);
    };









#endif
LstNode.cpp

    #include "header.h"

    int Node::cindex=0;

    Node::Node(void) :
      _next(this), _prev(this)
      {index=cindex++;}

      Node::~Node(void) {}
      Node* Node:: next(void)
      {
        return _next;  

       } 


     Node* Node::prev(void)
      {
        return _prev;  

       } 

     Node *Node::insert(Node*b){

         b->_next=_next;
         _next->_prev=b;
         b->_prev=this;
         _next=b;
         return b;

         }

    Node*Node::remove(void)
    {

    _prev->_next=_next;
    _next->_prev=_prev;
    _next->_prev=this;
    return this;    
    }
#include "header.h"

template <class T> List <T> :: List(void): _length(0)  //constructor for list
{
    header =new ListNode<T>(NULL); //mind you this uses the LIstNode class 
    win=header;

}

template<class T>  List <T>::~ List(void) // weird destructor
{
while (length()>0) {

    first();remove();
    }   
    delete header;

}

template <class T> T List <T> ::insert(T val)
{

win->insert( new ListNode <T> (val));
++_length;
return val;
}

template <class T> T List <T>::prepend(T val)
{   
    header->insert(new ListNode <T> (val));
    ++_length;
    return val;
}   

template <class T> T List <T>::append(T val)
{   
    header->prev()->insert(new ListNode <T> (val));
    ++_length;
    return val;
}

template<class T> List <T>* List <T>::append(List<T>*l)
{
        ListNode<T> *a =(ListNode<T>*)header->prev();
        a->splice(l->header);
        _length+=_length;
        l->header-remove();
        l->_length=0;
        l->win=header;
        return this;

}

template <class T> void List<T>::val(T v)
{
if (win!=header)
        win->_val=v;

}

template <class T> T List<T>::val(void)
{
return win->_val;   
}

template <class T> T List <T>:: next(void)
{
    win=(ListNode <T>*)win->next();
    return win->_val;

}

template <class T> T List <T>:: prev(void)
{
    win=(ListNode <T>*)win->prev();
    return win->_val;

}

template <class T> T List<T>::first(void)
{
win=(ListNode <T>*)header->next();
return win->_val;   

}

template <class T> T List<T>::last(void)
{
win=(ListNode <T>*)header->prev();
return win->_val;   

}

template <class T> int List <T>::length(void)
{
return _length; 
}

template< class T> bool List <T> ::isFirst(void)
{
    return (win==header->next()) &&(_length>0);
}

template< class T> bool List <T> ::isLast(void)
{
    return (win==header->prev()) &&(_length>0);
}

template <class T> bool List <T>::isHead(void)
{
    return (win == header);
}
#include <iostream>
#include "header.h"


int main()
{

List <int> dunder;
dunder.insert(9);
return 0;
}

您需要在同一文件中定义模板类及其定义。到目前为止,模板类标题和定义是不可分离的。

您需要定义模板类,并将其定义放在同一个文件中。到目前为止,模板类头文件和定义是不可分离的。

因此,模板类方法不能在定义它的头文件之外声明?只有在头文件底部显式包含该.cpp文件时才能声明。这样,头和定义最终都在同一个文件中。现在,我认为您应该在头文件中定义它,这样就不能在定义它的头文件之外声明模板类方法了?只有在头文件的底部显式地包含该.cpp文件时,才能这样做。这样,头和定义最终都在同一个文件中。现在我认为您应该在头文件中定义它
experiment.o: In function `main':
experiment.cpp:(.text+0x11): undefined reference to `List<int>::List()'
experiment.cpp:(.text+0x22): undefined reference to `List<int>::insert(int)'
experiment.cpp:(.text+0x33): undefined reference to `List<int>::~List()'
experiment.cpp:(.text+0x46): undefined reference to `List<int>::~List()'
collect2: error: ld returned 1 exit status