在'*'标记之前缺少模板参数

时间:2013-09-27 17:03:11

标签: c++

难以获得主要方法进行编译。 错误在第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);

1 个答案:

答案 0 :(得分:7)

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

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

而不是

chain *myChain=new chain(10);
例如,

如果你想将这个链用于字符串。