简体   繁体   English

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

[英]How do I implement "template" in classes? Is there something wrong with my code?

Question:题:

I am trying to implement "template" in my Queue classes.我正在尝试在我的队列类中实现“模板”。 I am getting errors.我收到错误。 I am not sure if that is syntax or my implementation of "templates" has issue.我不确定这是语法还是我的“模板”实现有问题。 Without "template", my program runs perfectly.没有“模板”,我的程序运行完美。

Question: I am not sure if the "template" implementation is the way it should be.问题:我不确定“模板”实现是否应该是这样。 Appreciate the comments and feedbacks.感谢评论和反馈。

This is SNode class这是 SNode 类

#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) {}

This is my SQueue class which这是我的 SQueue 类

#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;
}

This is my main/ Test file.这是我的主要/测试文件。

#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  

First, your node...首先,您的节点...

//...
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>;
};

Now, you are delcaring a class with a private default construtor... That's a very strong indication that SNode should be declared within SQueue, as in:现在,您正在声明一个具有私有默认构造函数的类......这是一个非常强烈的迹象,表明 SNode 应该在 SQueue 中声明,如下所示:

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.
};

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

相关问题 如何才能成为比赛条件。 还是我的代码有问题 - How it can be a race condition. Or it is something wrong with my code 我的make_unique有什么问题,如何实现以适应用例 - What is wrong with my make_unique, how do I implement to suit my usecase 使用c ++,我如何实现自己的容器类的itetator,以便我可以使用类似std :: sort() - using c++, how do I implement itetator to my own container class so i can use something like std::sort() 我的基本零初始化代码有问题吗? - is there something wrong with my basic zero initialization code? 我不明白我的代码有什么问题(指针和模板) - I don't understand what is wrong with my code (pointers and template) 我的清理代码有问题吗? (OpenGL + SDL) - Is there something wrong with my cleanup code? (OpenGL + SDL) 如果用户遇到问题,如何停止程序? - How do I stop a program if the user gets something wrong? 如何在整个嵌套类中实现模板? - How to implement template throughout nested classes? 如何正确实现其成员同时从 Cuda/C++ 中的主机和设备代码调用的类? - How do I properly implement classes whose members are called both from host and device code in Cuda/C++? 在没有多线程的情况下我的代码更快是正常的,我做错了什么吗? - It is normal that my code is faster without multithreading o am i doing something wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM