错误C2143:语法错误:'const'之前缺少','

时间:2018-02-23 05:12:46

标签: c++ visual-studio

template <class E, class K>
class SortedChain {
public:
    SortedChain() { first = 0; }
    ~SortedChain();
    bool IsEmpty() { return (first == 0); }
    //int Length() const;
    //bool Search (const K& k, const E& e);
    SortedChain<E, K>& Delete(const K& k, E& e);
    SortedChain<E, K>& Insert(const K& k, const E& e);
    ***SortedChain<E, K>& Merge(SortedChain<E, K> & S2 const);***
    void Output() const;
private:
    Node<E, K> *first;
};

sortedchain merge给出了以下错误:

  • 错误C2143:语法错误:'const'
  • 之前缺少','
  • 注意:请参阅正在编译的类模板实例化'SortedChain'的引用
  • 错误C4430:缺少类型说明符 - 假定为int。注意:C ++不支持default-int

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

const的展示位置只有错误 如果你想让参数引用常量,那么使用下面的

template <class E, class K>
class SortedChain {
public:
    SortedChain() { first = 0; }
    ~SortedChain();
    bool IsEmpty() { return (first == 0); }
    //int Length() const;
    //bool Search (const K& k, const E& e);
    SortedChain<E, K>& Delete(const K& k, E& e);
    SortedChain<E, K>& Insert(const K& k, const E& e);
    SortedChain<E, K>& Merge(const SortedChain<E, K> & S2);// change this
    void Output() const;
private:
    Node<E, K> *first;
};

你可以让这个函数为类对象保持不变,然后使用这个

template <class E, class K>
class SortedChain {
public:
    SortedChain() { first = 0; }
    ~SortedChain();
    bool IsEmpty() { return (first == 0); }
    //int Length() const;
    //bool Search (const K& k, const E& e);
    SortedChain<E, K>& Delete(const K& k, E& e);
    SortedChain<E, K>& Insert(const K& k, const E& e);
    SortedChain<E, K>& Merge(SortedChain<E, K> & S2) const;// change this
    void Output() const;
private:
    Node<E, K> *first;
};