1

难以获得编译的主要方法。错误在第 10 行:

missing template argument before * token.
myChain was not  declared  in this scoop
expected type-specifier before 'chain'
expected ';' before 'chain'

下面是发生错误的代码。

#include <iostream>
#include "chain.h"
#include "IOcode.h"
#include "chainNode.h"

using namespace std;

int main (){

    chain *myChain=new chain(10);
    userInputOutput(myChain, "chain");
}

chainNode.h 依赖

#ifndef chainNode_
#define chainNode_

template <class T>
struct chainNode 
{
   // data members
   T element;
   chainNode<T> *next;

   // methods
   chainNode() {}
   chainNode(const T& element)
      {this->element = element;}
   chainNode(const T& element, chainNode<T>* next)
      {this->element = element;
       this->next = next;}
};

#endif

链的chain.h 依赖类定义。

#ifndef chain_
#define chain_

#include<iostream>
#include<sstream>
#include<string>
#include "linearList.h"
#include "chainNode.h"
#include "myExceptions.h"

class linkedDigraph;
template <class T> class linkedWDigraph;

template<class T>
class chain: public linearList
{
   friend class linkedDigraph;
   friend class linkedWDigraph<int>;
   friend class linkedWDigraph<float>;
   friend class linkedWDigraph<double>;
   public:
      // constructor, copy constructor and destructor
      chain(int initialCapacity = 10);
      chain(const chain<T>&);
      ~chain();

      // ADT methods
      bool empty() const {return listSize == 0;}
      int size() const {return listSize;}
      T& get(int theIndex) const;
      int indexOf(const T& theElement) const;
      void erase(int theIndex);
      void insert(int theIndex, const T& theElement);
      void output(ostream& out) const;

   protected:
      void checkIndex(int theIndex) const;
            // throw illegalIndex if theIndex invalid
      chainNode<T>* firstNode;  // pointer to first node in chain
      int listSize;             // number of elements in list
};

IOcode.h 依赖

#include <iostream>
#include "linearList.h"

using namespace std;

void userInputOutput (linearList* l, string dataStructure);
4

1 回答 1

8

这个类是模板化的,这意味着你必须指定一个类型来填充模板,所以你会说

chain<string> *myChain=new chain<string>(10);

代替

chain *myChain=new chain(10);

例如,如果您想将此链用于字符串。

于 2013-09-27T17:06:12.907 回答