none
C++ 模板类静态成员继成问题 RRS feed

  • 问题

  • /************************************************** * Essential C++ -- Stanley Lippman * Addison-Wesley * ISBN 0-201-48518-4 * homepage: www.objectwrite.com * email: slippman@objectwrite.com *************************************************/ #include <string> #include <vector> #include <utility> #include <iostream> using namespace std; template <int len, int beg_pos> class num_sequence; template <int len, int beg_pos> ostream& operator<<(ostream &os, const num_sequence<len, beg_pos> &ns); template <int len, int beg_pos> class num_sequence { public: virtual ~num_sequence() {}; int elem(int pos) const; const char* what_am_i() const; static int max_elems() { return _max_elems; } ostream& print(ostream &os = cout) const; protected: virtual void gen_elems(int pos) const = 0; bool check_integrity(int pos, int size) const; num_sequence(vector<int> *pe) : _pelems(pe) {} // static const int _max_elems = 1024; // ok, but vc++ doesn't accept enum { _max_elems = 1024 }; vector<int> *_pelems; }; template <int len, int beg_pos> ostream& operator<<(ostream &os, const num_sequence<len, beg_pos> &ns) { return ns.print(os); } template <int len, int beg_pos> int num_sequence<len, beg_pos>:: elem(int pos) const { if (!check_integrity(pos, _pelems->size())) return 0; return (*_pelems)[pos - 1]; } #include <typeinfo> template <int length, int beg_pos> const char* num_sequence<length, beg_pos>:: what_am_i() const { return typeid(*this).name(); } template <int length, int beg_pos> bool num_sequence<length, beg_pos>:: check_integrity(int pos, int size) const { if (pos <= 0 || pos > max_elems()) { cerr << "!! invalid position: " << pos << " Cannot honor request\n"; return false; } if (pos > size) gen_elems(pos); return true; } template <int length, int beg_pos> ostream& num_sequence<length, beg_pos>:: print(ostream &os) const { int elem_pos = beg_pos - 1; int end_pos = elem_pos + length; if (!check_integrity(end_pos, _pelems->size())) return os; os << "( " << beg_pos << " , " << length << " ) "; while (elem_pos < end_pos) os << (*_pelems)[elem_pos++] << ' '; return os; } template <int length, int beg_pos = 1> class Fibonacci : public num_sequence<length, beg_pos> { public: Fibonacci() : num_sequence<length, beg_pos>(&_elems) {} //note: This is constructor's initial list protected: virtual void gen_elems(int pos) const; //note: const static vector<int> _elems; }; template <int length, int beg_pos> vector<int> Fibonacci<length, beg_pos>::_elems; template <int length, int beg_pos> void Fibonacci<length, beg_pos>:: gen_elems(int pos) const { //if (pos <= 0 || pos > num_sequence<length, beg_pos>::max_elems()) if (pos <= 0 || pos >max_elems()) return; if (_elems.empty()) { _elems.push_back(1); _elems.push_back(1); } if (_elems.size() < pos) { int ix = _elems.size(); int n_2 = _elems[ix - 2], n_1 = _elems[ix - 1]; int elem; for (; ix < pos; ++ix) { elem = n_2 + n_1; // cout << "gen_elems: " << elem << endl; _elems.push_back(elem); n_2 = n_1; n_1 = elem; } } } int main() { /* fib1: ( 1 , 8 ) 1 1 2 3 5 8 13 21 fib2: ( 8 , 8 ) 21 34 55 89 144 233 377 610 fib3: ( 8 , 12 ) 21 34 55 89 144 233 377 610 987 1597 2584 4181 */ Fibonacci<8> fib1; Fibonacci<8, 8> fib2; Fibonacci<12, 8> fib3; cout << "fib1: " << fib1 << '\n' << "fib2: " << fib2 << '\n' << "fib3: " << fib3 << endl; return 0; // quiets vc++ }

    VS2015可以通过

    但VS2017出错

    1>------ 已启动生成: 项目: 6.例子6, 配置: Debug Win32 ------
    1>源.cpp
    1>e:\c++学习\source\repos\6.例子6\6.例子6\num_sequence.h(72): error C3861: “max_elems”: 找不到标识符
    1>已完成生成项目“6.例子6.vcxproj”的操作 - 失败。
    ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


    2018年6月14日 1:24

答案

  • 项目属性中:C/C++——语言——符合模式:改为否 

    即可

    因为不完全符合语法标准,类模板中要用:this->max_elems(),这样编译器就懂去哪找它了,如果不改符合模式,加上this->即可


    2018年6月14日 2:45

全部回复

  • 项目属性中:C/C++——语言——符合模式:改为否 

    即可

    因为不完全符合语法标准,类模板中要用:this->max_elems(),这样编译器就懂去哪找它了,如果不改符合模式,加上this->即可


    2018年6月14日 2:45
  • Hi,

    看上去你的问题已经得到解决。请标记为答复方便他人找到解决方案。

    谢谢你的理解与支持。

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年6月15日 1:16