我在家庭作业的问题上遇到了问题。我无法使enqueue()
或dequeue()
函数正常工作。 visual Studio表示该类没有名称成员。我已经包含了t指令和我的所有代码。
我不确定代码中的其他所有内容是否都正确。
说明 编写如下定义的模板队列类: 私有数据成员:STL列表 公共成员职能: -empty -尺寸 -enqueue -deque -面前 -背部 然后编写一个驱动程序来测试上面的队列类。
我不知道标题是否正确,但这是我能想到的。
//Header File
#include <iostream>
#include <list>
#include <queue>
using namespace std;
template<typename queueElement>
class Queue
{
public:
bool empty() const;
queueElement size();
void enqueue(const queueElement & value);
void dequeue();
queueElement front();
queueElement back();
private:
list <queueElement> aList;
};
template<typename queueElement>
inline bool Queue<queueElement>::empty() const
{
return aList.empty();
}
template<typename queueElement>
queueElement Queue<queueElement>::size()
{
return size;
}
template<typename queueElement>
void Queue<queueElement>::enqueue(const queueElement & value)
{
aList.push_back(value);
}
template<typename queueElement>
void Queue<queueElement>::dequeue()
{
if (!aList.empty())
{
aList.pop_front();
}
else
{
cout << "Queue is Empty!" << endl;
}
}
template<typename queueElement>
queueElement Queue<queueElement>::front()
{
return aList.front();
}
template<typename queueElement>
queueElement Queue<queueElement>::back()
{
return aList.back();
}
这是我要测试的驱动程序,它是出现错误的地方。
//Driver
#include <iostream>
#include <queue>
#include <list>
#include "Header.h"
using namespace std;
int main()
{
list<int> intList;
for (int i = 0; i < 20; i++)
{
intList.enqueue(i); //problem with enqueue
}
cout << "Size: " << intList.size() << endl;
intList.dequeue(); //problem with dequeue
intList.dequeue(); //problem with dequeue
cout << "Size after dequeue: " << intList.size() << endl;
cout << "Front: " << intList.front() << endl;
cout << "Back: " << intList.back() << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
我在家庭作业的问题上遇到了问题。我不能让我的入队或出队功能起作用。
请定义“问题”并解释你的意思是“我不能让我的入队或出队功能”,“入队问题”和“出队问题”
无论如何,我希望你向我们展示size()
方法
template<typename queueElement>
queueElement Queue<queueElement>::size()
{
return size;
}
是转录错误。
如果您只是简单地返回size
,我认为您要返回size()
方法本身的地址,该地址已转换为queueElement
类型(在int
中转换),在您身上例子)。
我认为size()
应该返回aList.size()
,一个无符号整数(我假设为std::size_t
),而不是queueElement
。并且,也许,可以是const
方法。
某事
template<typename queueElement>
std::size_t Queue<queueElement>::size() const
{
return aList.size();
}
答案 1 :(得分:0)
两个拼写错误,您可能打算将Queue
实例化为enqueue
,因为dequeue
,Queue
等是Queue<int> intList;
的成员。
size()
另一个是因为aList.size()
应该返回template<typename queueElement>
queueElement Queue<queueElement>::size()
{
return aList.size();
}
。
x = np.array(x_range)
print('equation types')
print('1) y=mx+b')
print('2) y=a*sin(b*(x+c))')
print('3) y=ax^2 + bx + c')
#...etc.
eq = int(input('equation type number: '))
if eq == 1:
m = float(input('m'))
b = float(input('b'))
y = m*x+b
if eq == 2:
#...etc.
plt.plot(x, y)
plt.show()