错误:调用"Queue::Queue()"没有匹配函数

error: no matching function for call to "Queue::Queue()"

本文关键字:Queue 函数 调用 错误      更新时间:2023-10-16

所以我正在尝试在我的主文件中调用一个函数.cpp但我得到"错误:没有匹配的函数来调用'队列::队列()"。

队列.h

#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
class Queue
{
    public:
        Queue(int);
        ~Queue();
        //circular queue methods
        void enqueue(std::string);
        std::string dequeue(); //should send through network, call transmit msg
        void printQueue();
        bool queueIsFull(); //send when full
        bool queueIsEmpty(); //send when empty
    protected:
    private:
        int queueSize;
        int queueHead;
        int queueTail;
        int queueCount;
        std::string *arrayQueue;
};
#endif // QUEUE_H

队列.cpp

#include "Queue.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
Queue::Queue(int qs)
{
    queueSize = qs;
    arrayQueue = new string[queueSize];
    queueHead = 0;
    queueTail = 0;
}
Queue::~Queue()
{
    delete[] arrayQueue;
}
void Queue::enqueue(string word)
{
    for (int i=0;i<10;i++)
    {
        arrayQueue[i] = word;
    }
}
void Queue::printQueue()
{
    for(int j=0;j<10;j++)
    {
        cout<<arrayQueue[j]<<endl;
    }
}

主.cpp

#include <iostream>
#include "Queue.h"

using namespace std;
int main()
{
    int userChoice;
    Queue q;
    while(2==2)
    {
        cout<<"======Main Menu======"<<endl;
        cout<<"1. Enqueue word"<<endl;
        cout<<"2. Dequeue word"<<endl;
        cout<<"3. Print queue"<<endl;
        cout<<"4. Enqueue sentence"<<endl;
        cout<<"5. Quit"<<endl;
        cin>>userChoice;
        if (userChoice == 1)
        {
            string enqueueWord;
            cout<<"word: ";
            cin>>enqueueWord;
            enqueue(enqueueWord);
        }
        if (userChoice == 2)
        {
        }
        if (userChoice == 3)
        {
        }
        if (userChoice == 4)
        {
        }
        if (userChoice == 5)
        {
        }
    }
    return 0;
}

因此,为了从头文件调用该函数,我在int main()的开头做了"Queue q;",然后当我需要调用该函数时,我做了"q.enqueue(enqueueWord)"。我也尝试只做"Queue::enqueue(enqueueWord),但这也没有用,我得到了一个不同的错误。我觉得这是一个简单的解决方案,但我就是想不通。感谢您的帮助,并随时要求我澄清任何事情。

Queue q;

尝试调用默认构造函数Queue::Queue 。但是,此构造函数已被自动删除,因为您自己显式声明了一个构造函数,即 Queue::Queue(int)

初始化时将适当的参数传递给q,例如

Queue q1(42);    // pre-C++11 syntax
Queue q{42};     // available since C++11

注意:42在这里只是一个示例值。

还可以使用默认参数使定义保持原样,并使用默认值初始化对象。


笔记:

  • 为什么while(2==2)while (true)是常见的方式。