简体   繁体   English

派生类的成员函数不关心模板参数

[英]Member function of derived class does not care about template argument

I want to simulate a chain of particles either in the up position or the down position. 我想模拟向上或向下位置的粒子链。 To this end I made a class that inherits from bitset. 为此,我制作了一个从位集继承的类。 It looks like: 看起来像:

#include <bitset>
using namespace std;

template <size_t N>
class State : public bitset<N> {
public:
  State<N>();
  State<N>(size_t b);

  long int E();
private:
  size_t length;
};

template<unsigned long N>
State<N>::State()
  : std::bitset<N>(), length(N)
{}

template<unsigned long N>
State<N>::State(size_t b)
  : std::bitset<N>(b), length(N)
{}

Once such an object is instantiated using a certain length, I would like to find the energy associated to such an object. 一旦使用一定的长度实例化了这样的对象,我想找到与该对象相关的能量。 I want to do this 我想做这个

#include "state.h"

long int State::E(){
  long int e = 0;
  for (size_t i = 1; i != length; ++i)
    e += (test[i] == test[i - 1]) ? 1 : -1;

  return e;
}

I receive the error 我收到错误

state/state.cc:3:10: error: ‘template<long unsigned int N> class State’ used without template parameters
 long int State::E(){
          ^~~~~
state/state.cc: In function ‘long int E()’:
state/state.cc:5:27: error: ‘length’ was not declared in this scope
   for (size_t i = 1; i != length; ++i)
                           ^~~~~~
state/state.cc:6:11: error: ‘test’ was not declared in this scope
     e += (test[i] == test[i - 1]) ? 1 : -1;
           ^~~~

I understand this to mean that the compiler does not recognize that E() is a member function of my class due to a missing template argument. 我理解这意味着编译器由于缺少模板参数而无法识别E()是我的类的成员函数。 However, I was hoping there is a way that I can call sE() on a State<20> object for instance. 但是,我希望有一种方法可以例如对State<20>对象调用sE() So once the object is instantiated I would like to be able to call E() without having to again specify the size. 因此,一旦实例化了对象,我希望能够调用E()而不必再次指定大小。 Is this possible? 这可能吗? Thanks in advance! 提前致谢!

There are two issues here: defining the member function E() of the class template State<N> , and accessing the test() member function of the dependent base class bitset<N> . 这里有两个问题:定义类模板State<N>的成员函数E() ,以及访问从属基类 bitset<N>test()成员函数。

template<size_t N>
long int State<N>::E(){
  long int e = 0;
  for (size_t i = 1; i != length; ++i)
    e += (this->test(i) == this->test(i - 1)) ? 1 : -1;
}

Note both the template<size_t N> and the State<N> as well as the this-> in front of test . 注意test前面的template<size_t N>State<N>以及this-> See this Q&A for a detailed explanation. 请参阅此问答以获取详细说明。

Final note: also be careful: it's test() (parentheses) and operator[] (brackets) in std::bitset . 最后说明:也要小心: std::bitsettest() (括号)和operator[] (括号)。

In the definition of member function of tempalte class, you must specify the template parameters as you did for the constructor. 在tempalte类的成员函数的定义中,必须像对构造函数那样指定模板参数。

The error "lenght not declared" should be fixed also by this change. 错误“未声明长度”也应通过此更改来修复。

template <size_t N>
long int State<N>::E(){
  long int e = 0;
  for (size_t i = 1; i != length; ++i)
    e += (test(i) == test(i - 1)) ? 1 : -1;

  return e;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM