简体   繁体   English

C ++ | 无法推导T的模板参数| 没有参数的虚函数(display())

[英]C++ | Could not deduce template argument for T | Void function with no arguments (display() )

I am creating a dynamic Que in C++ with templates for further practice. 我正在C ++中创建带有模板的动态Que,以供进一步练习。 Most important functions seem to work just fine except for last 2 I have defined - void functions with no arguments. 除了我定义的最后2个功能外,最重要的功能似乎都可以正常工作-没有参数的void函数。

#include <iostream>
#include <string>
using namespace std;


template <typename T>
class DynamicQueue
{
private:
// Structure for the queue nodes
struct QueueNode
{
    T value;       // Value in a node
    QueueNode *next;    // Pointer to the next node
};
QueueNode *top;      // The top of the queue
QueueNode *bottom;     // The bottom of the queue
int numItems;          // Number of items in the queue
public:
DynamicQueue() {
    top = nullptr;
    bottom = nullptr;
    numItems = 0;
}
~DynamicQueue() {
    delete[] top;
    delete[] bottom;
}
template <typename T>
    bool isEmpty() const {
    bool empty;
    if (numItems > 0)
        empty = false;
    else
        empty = true;
}
template <typename T>
void display() const {
        QueueNode* temp = top;
        while (temp != nullptr) {
            cout << temp->value << endl;
            temp=temp->next;
        }
}
template <typename T>
void clear() {
    T temp;
    while (!isEmpty()) {
        dequeue(data);
    }
}
};

There is an enque and a deque functions that I did not include in the code as they are working just fine. 我没有在代码中包含enque和deque函数,因为它们工作得很好。 The main function looks kind of like: 主要功能看起来像:

int main() {

string str;
int number;

DynamicQueue<string> strQ;
DynamicQueue<int> intQ;

strQ.enqueue("Word number 1");
strQ.enqueue("2");

strQ.dequeue("2");
strQ.display();

intQ.enqueue(1);
intQ.enqueue(2);
int placehold;
intQ.dequeue(placehold);
intQ.display();

strQ.clear();
intQ.clear();

getchar();
return 0;
}

Compiler produces the following error: 编译器产生以下错误:

error C2672: 'DynamicQueue::display': no matching overloaded function found 错误C2672:“ DynamicQueue :: display”:找不到匹配的重载函数

error C2783: 'void DynamicQueue::display(void) const': could not deduce template argument for 'T' 错误C2783:“无效DynamicQueue :: display(void)const”:无法推断出“ T”的模板参数

note: see declaration of 'DynamicQueue::display' 注意:请参见“ DynamicQueue :: display”的声明

I understand that the issue is somewhere in me doing the templates wrong. 我知道问题出在我做模板错误的地方。 But I dont seem to understand the correct way of doing it. 但我似乎并不了解正确的做法。

(There are the same issues for the clear() function) (clear()函数存在相同的问题)

You seem to have a template shadowing issue because you are redeclaring a template T over your class methods definition, which is in your case redundant. 您似乎遇到了模板屏蔽问题,因为您正在通过类方法定义重新声明模板T ,这在您的情况下是多余的。

It would have been necessary if you had implemented your methods outside your class definition. 如果您在类定义之外实现了方法,那将是必要的。

Inside class definition: 内部类定义:

void clear() {
    // Stuff
}

Outside: 外:

template <typename T>
void DynamicQueue<T>::clear() {
    // Stuff
}

The syntax you initially used is a new template declaration specific for a method, for example to enqueue it could be: 您最初使用的语法是特定于方法的新模板声明,例如,使它入队的语法可能是:

template<typename... Args>
void enqueue(Args&&... args)
{
    // Create new node
    QueueNode* new_node = new QueueNode();
    bottom->next = new_node;

    new (&(new_node->value)) T(args...);
    bottom = new_node;
}

And the outside declaration: 和外部声明:

template<typename T>
template<typename... Args>
void DynamicQueue<T>::enqueue(Args&&... args)
{
    // Create new node
    QueueNode* new_node = new QueueNode();
    bottom->next = new_node;

    new (&(new_node->value)) T(args...);
    bottom = new_node;
}

but the template argument's name cannot be T or you'll have shadowing issue 但是模板参数的名称不能为T,否则会出现阴影问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM