I have a question about the c++ template function extinction. Monsters, please crying

Asked 1 years ago, Updated 1 years ago, 75 views

#pragma once
template <typename Elem>
class NodeList{
public:
    class Node {
    public:
        Elem elem;
        Node* prev;
        Node* next;

    };
    //template <typename Elem> friend class NodeSequence;
public :
    Elem& operator*() { return v->elem; };
    bool operator==(const Node* p) const { return v == p.v; };
    bool operator!=(const Node* p) const { return v != p.v; };
    Node* operator++() { v = v->next; return *this; };
    Node* operator--() { v = v->prev; return *this; };
private: 
    Node* v;
public:
    NodeList() {
        n = 0;
        header = new Node;
        trailer = new Node;
        header->next = trailer;
        trailer->prev = header;
    };
    virtual ~NodeList() {
        while (!empty()) {
            eraseFront();
        }
    };
    int size() const {
        return n;
    };
    bool empty() const {
        return (n == 0);
    };
    Node* begin() const {
        return header->next;
    }
    ;
    Node* end() const {
        return (trailer);
    };
    void insertFront(const Elem& e) { insert(begin(), e); };
    void insertBack(const Elem& e) { insert(end(), e); };
    void insert( Node* const p,const Elem& e) {
        Node*w = p;
        Node*u = w->prev;
        Node*v = new Node;
        v->elem = e;
        v->next = w; w->prev = v;
        v->prev = u; u->next = v;
        n++;
    };
    void eraseFront() {
        erase(begin());
    };
    void eraseBack() {
        erase(--end());
    };
    void erase(Node* const p) {
        Node* w = p->next;
        Node* u =p->prev;
        u->next = w; w->prev = u;
        delete v;
        n--;
    };
private:
    int n;
    Node* header;
    Node* trailer;
};
template <typename Elem>
class NodeSequence :public NodeList<Elem> {
public:
    NodeSequence() {
        typename NodeSequence::NodeList::NodeList(); };
     ~NodeSequence() { typename NodeSequence::NodeList::~NodeList<Elem>(); };
    typename NodeList<Elem>::Node* atIndex(int i) const {
        typename NodeSequence::NodeList::Node* p= typename NodeSequence::begin();
        for (int j = 0; j < i; j++)++p;
        return p;
    };
    int indexof(const typename NodeList<Elem>::Node* p)const {
        typename NodeSequence::NodeList::Node* q = typename NodeSequence::begin();
        int j = 0;
        while (q != p) {
            ++q; ++j;
        }
        return j;
    }
};
#include <iostream>
using namespace std;

#include "sequence.h"

void main() {
    typedef NodeSequence<int> IntSequence;
    typedef NodeSequence<int>::Node* IntSequenceNodePtr;
    int a[] = { 6, 7, 2, 5, 1, 4, 3 };
    IntSequence ns;
    for (int i = 0; i < 7; i++) ns.insertFront(a[i]);
    cout << "A sequence of size = " << ns.size() << endl;
    system("pause");
}

The first code is sequence.h header file, and the second is main door CPP file. I am trying to create a constructor and a destructor in the sequence class of the header file, but this error appears ㅠㅠ 어떻게 What should I do?

c++ template

2022-09-22 13:14

1 Answers

Please edit it and compile it as below.

NodeSequence() {
        typename NodeSequence::NodeList::template NodeList<Elem>(); };
     ~NodeSequence() { typename NodeSequence::NodeList::template ~NodeList<Elem>(); };

For more information, search for C7510 at the link below to find out the following.

https://docs.microsoft.com/ko-kr/cpp/cpp-conformance-improvements-2017?view=vs-2017

Template keywords and nested name specifiers In /permission- mode, the compiler requires the template keyword to precede the template name if it follows a dependent nested name specifier. In /permission- mode, the following code must be used with C7510: 'foo': dependent template name with 'template'. Note: Please see reference to class template instantiation 'X' being compiled. Create the .


2022-09-22 13:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.