首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在类中实现“模板”?我的代码有什么问题吗?

如何在类中实现“模板”?我的代码有什么问题吗?
EN

Stack Overflow用户
提问于 2021-10-27 03:57:12
回答 1查看 66关注 0票数 0

问题:

我正在尝试在我的队列类中实现“模板”。我收到了错误。我不确定是语法问题还是我的“模板”实现有问题。没有“模板”,我的程序运行得很好。

问:我不确定“模板”实现是否是它应该采用的方式。感谢您的评论和反馈。

这是SNode类

代码语言:javascript
复制
#include <string>
using namespace std;

template <class T>
class SNode {
private:
    int elem;
    T* next;
    explicit SNode();
    //friend class SQueue<T>;
};

template <class T>
SNode<T>::SNode() : elem(" "), next(nullptr) {}

这是我的SQueue类,它

代码语言:javascript
复制
#include <string>
#include "SNode.h"
using namespace std;

template <class T>
class SQueue {
public:
    SQueue();
    void enqueue(T);
    void dequeue();
    void print();

private:
    T* front;
    T* end;
};

template <class T>
SQueue<T>::SQueue() : front(NULL), end(NULL) {}

template <class T>
void SQueue<T>::enqueue(T e) {
    T* np = new T();
    np->elem = e;
    np->next = NULL;
    if (front == NULL && end == NULL) {
        front = end = np;
        return;
    }
    end->next = np;
    end = np;
}

template <class T>
void SQueue<T>::dequeue() {
    T* np = front;
    if (front == NULL) {
        cout << "The queue is empty-1!" << endl;
        return;
    }
    front = front->next;
    delete np;
}

template <class T>
void SQueue<T>::print() {
    T* np = front;
    if (front == NULL) {
        cout << "The queue is empty-2!" << endl;
        return;
    }
    for (T* temp = front; temp != NULL; temp = temp->next) {
        if (temp != front) {
            cout << " <- ";
        }
        cout << temp->elem;
    }
    cout << "  [Queue: FIFO- First In First Out]" << endl;
}

这是我的main/ Test文件。

代码语言:javascript
复制
#include <iostream>
#include "SQueue.h"

using namespace std;
int main() {
    typedef SQueue<int> SQueue;
    SQueue SQ;
    cout << "Queue 1: ";
    SQ.enqueue(1);
    SQ.enqueue(2);
    SQ.enqueue(3);
    SQ.enqueue(3);
    SQ.print();
    SQ.dequeue();
    SQ.dequeue();
    SQ.print();
return 0;
}

ERROR MESSAGE
Severity    Code    Description Project File    Line    Suppression State
Error   C2227   left of '->elem' must point to class/struct/union/generic type  DeleteLinkedList    SQueue.h    24  
EN

回答 1

Stack Overflow用户

发布于 2021-10-27 04:37:20

首先,您的节点...

代码语言:javascript
复制
//...
template <class T>   // I assume T is the as-yet-unknown type stored in the node.
class SNode {
private:
    int elem;  // Why int ??
    T* next;   // Why T*

    // Should be:
    T elem;                // The user data 
    SNode<T>* next;        // the next node.

    explicit SNode();      // Why explicit?  Do you have a _very_ good reason for that? 

    // Should read:
    SNode() : elem(T()), next(nullptr) {}

    friend class SQueue<T>;
};

现在,您正在处理一个具有私有默认构造器的类...这是一个非常强烈的迹象,表明应该在SQueue中声明SNode,如下所示:

代码语言:javascript
复制
template<class T> 
class SQueue
{
public:
    // Ideally, this inner class should be declared 'private', and SQueue
    // should declare some form of iterator to hide these details from
    // the caller... It's best to get everything else running first, and
    // add this complexity on a solid base.
    // 
    struct SNode  // all members are public.
    {
        SNode() : elem(T()), next(nullptr) {}
        T elem;
        SNode* next;            
    };

public:
    // define your constructors and access functions..

private:
    SNode* front;  // the queue stores a single-linked list of nodes. 
                   // Only one pointer is necessary to do that.
                   // UNLESS you want to create a list that's optimized for 
                   // appending elements at the end.  But optimizations should be 
                   // done after the unoptimized code has been tested and proven                
                   // to work flawlessly.
};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69732585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档