错误:调用"Queue::Queue()"没有匹配函数
error: no matching function for call to "Queue::Queue()"
所以我正在尝试在我的主文件中调用一个函数.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)
是常见的方式。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 在 boost::lockfree:queue 默认构造函数的情况下断言失败
- 错误:调用"Queue::Queue()"没有匹配函数
- 当<T> T 没有复制构造函数时,std::queue 的虚拟包装器不会编译
- c++ std::queue pop()和析构函数