没有参数列表的模板名称使用无效

invalid use of template-name w/o an argument list

本文关键字:无效 参数 列表      更新时间:2023-10-16

这是我的代码,我不断收到错误./partition2.h:45:5:错误:在没有参数列表的情况下无效使用模板名称"fsu::P artition2",但我不确定是什么导致了此错误。通常是因为我没有在我的函数之前放置模板,但现在我有点困惑。

#include "vector.h"
#include <vector>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <entry.h>
#include <list.h>
#include <primes.h>
namespace fsu
{
  template < typename N = size_t >
  class Partition2
    {
      public:
      explicit Partition2     ( N size );       // create singletons {0} .. {size-1}
      void     Reset         ();               // reverts to singletons
      void     Reset         ( N newsize );
      void     PushSingleton () { parent_.PushBack((N)parent_.Size()); rank_.PushBack(0); }
      void     Union         ( N x , N y ) { Link(Root(x),Root(y)); }
      bool     Find          ( N x , N y ) { return Root(x) == Root(y); }
      bool     Find          ( N x , N y ) const { return Root(x) == Root(y); }
      size_t   Size          () const { return rank_.Size(); }
      size_t   Components    () const;
      void     Display       ( std::ostream& os ) const;
      void     Dump          ( std::ostream& os ) const;
      private: // methods
      N    Root   ( N x );                // path compression changes state
      N    Root   ( N x ) const;          // no path compression
      void Link   ( N root1 , N root2 );  // union assuming arguments are roots
      private: // objects
      fsu::Vector <N> parent_;
      fsu::Vector <N> rank_;
      N comp_count;
    };
  template < typename N = size_t >
    Partition2::Partition2 ( N size) : parent_((size_t)size,0), rank_((size_t)size, 0)
    {
    }
} //fsu

对于类模板成员的外联定义,需要在类名后使用模板参数。

  template < typename N = size_t >
    Partition2<N>::Partition2 ( N size) : // ...
    {
        // ...
    }

顺便说一句,我不建议使用 N 作为类型模板参数的名称,因为它通常用于整型(非类型)模板参数。