学生寻求帮助.... [英] Student seeking assistance....

查看:67
本文介绍了学生寻求帮助....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了一个类,堆栈可以使用。我创建了一个名为Job的类,我想把它推到堆栈上。


我似乎无法弄清楚为什么推送不起作用。 (我知道

堆栈有效,我已经看过它了。)


堆栈头看起来像这样:


< code>


#ifndef Stackll_h

#define Stackll_h


#include< stdlib .h>


//堆栈:头文件


模板< class Etype>

class Stack

{

public:


//构造函数

//

//输入:无

//目的:创建一个空Stack

//输出:无


Stack( );

//复制构造函数

//

//输入:Stack S

//目的:将Stack初始化为S

//输出:无

Stack(const Stack& S);

//析构函数

//

//输入:无

//目的:释放Stack的内存

//输出:无


~Stack();

//复制作业

//
//输入:堆栈S

//目的:将S分配给当前堆栈

//输出:当前堆栈


const Stack& operator =(const Stack& S);

//推

//

//输入:元素E

//目的:将E置于堆栈顶部

//输出:如果成功则为1; 0否则


int推(const Etype& E);

//流行

//

//输入:无

//目的:删除Stack的顶部元素

//输出:如果成功则输出1; 0否则


int Pop();

//检索

//

//输入:无

//目的:返回堆栈的顶部元素E

//输出:元素E和1如果成功; 0否则


int检索(Etype& E)const;

//空

//

//输入:无

//目的:检查Stack是否为空

//输出:如果为空则为1; 0否则


int Empty()const;

//清除

//

/ /输入:无

//目的:重新初始化Stack为空

//输出:无


void清除();

私人:


//数据成员


//单链接堆栈节点

struct StackNode

{

Etype元素;

StackNode *下一个;


// StackNode构造函数

StackNode():Next(NULL)

{}

StackNode(const Etype& E,StackNode) * P = NULL):

元素(E),下一个(P)

{}

};

//指向堆栈顶部的指针

StackNode *顶部;

};


#endif

< / code>


我想要推入堆栈的主要部分就是这样(部分

吧无论如何......)


< code>

//创造一份工作

工作* thisJob; < br $>
char menuChoice;

int success = 0;


do

{

system(" cls");

Stack< Job * JC;

mainMenu();


cin > menuChoice;

menuChoice = toupper(menuChoice);

cin.ignore(1,''\ n'');


开关(menuChoice)

{

案例''N'':

//分配值

thisJob = new Job;

assignJobValues(thisJob);

success = JC-> Push(& thisJob);


< / code>


我得到的错误是:没有匹配函数来调用

`Stack< Job> :: Push(Job **)''


感谢您的时间!

I have been given a class, stack to work with. I have created a class
called Job that I want to push on the stack.

I cannot seem to figure out why the push isn''t working. (I know the
stack works, I have seen it demonstrated.)

The stack header looks like this:

<code>

#ifndef Stackll_h
#define Stackll_h

#include <stdlib.h>

// Stack: Header File

template <class Etype>
class Stack
{
public:

// Constructor
//
// Input : None
// Purpose: To create an empty Stack
// Output : None

Stack ( );
// Copy constructor
//
// Input : Stack S
// Purpose: To initialize Stack to S
// Output : None

Stack ( const Stack & S );
// Destructor
//
// Input : None
// Purpose: To free memory of Stack
// Output : None

~Stack ( );
// Copy assignment
//
// Input : Stack S
// Purpose: To assign S to current Stack
// Output : Current Stack

const Stack & operator= ( const Stack & S );
// Push
//
// Input : Element E
// Purpose: To place E on the top of Stack
// Output : 1 if successful; 0 otherwise

int Push ( const Etype & E );
// Pop
//
// Input : None
// Purpose: To delete the top element of Stack
// Output : 1 if successful; 0 otherwise

int Pop ( );
// Retrieve
//
// Input : None
// Purpose: To return the top element E of Stack
// Output : Element E and 1 if successful; 0 otherwise

int Retrieve ( Etype & E ) const;
// Empty
//
// Input : None
// Purpose: To check if Stack is empty
// Output : 1 if empty; 0 otherwise

int Empty ( ) const;
// Clear
//
// Input : None
// Purpose: To re-initialize Stack to empty
// Output : None

void Clear ( );
private:

// Data members

// A singly-linked Stack node
struct StackNode
{
Etype Element;
StackNode *Next;

// StackNode constructors
StackNode ( ) : Next ( NULL )
{ }
StackNode ( const Etype & E, StackNode *P = NULL ) :
Element ( E ), Next ( P )
{ }
};

// Pointer to the top of Stack
StackNode *Top;
};

#endif
</code>

My main where I am trying to Push onto the stack looks like this (part
of it anyway...)

<code>
//create a job
Job *thisJob;
char menuChoice;
int success = 0;

do
{
system("cls");
Stack<Job*JC;
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, ''\n'');

switch(menuChoice)
{
case ''N'':
//assign values
thisJob = new Job;
assignJobValues(thisJob);
success = JC->Push(&thisJob);

</code>

The error I get is: no matching function for call to
`Stack<Job>::Push(Job**)''

Thanks for your time!

推荐答案

sa *** @ murdocks.on.ca 写道:

我有给了一个类,堆栈工作。我创建了一个名为Job的类,我想把它推到堆栈上。


我似乎无法弄清楚为什么推送不起作用。 (我知道

堆栈有效,我已经看过它了。)


堆栈头看起来像这样:


< code>


#ifndef Stackll_h

#define Stackll_h


#include< stdlib .h>


//堆栈:头文件


模板< class Etype>

class Stack < br $>
{

public:





//推送

//

//输入:元素E

//目的:将E置于堆栈顶部

//输出:如果成功则输出1; 0否则


int推(const Etype& E);




};


#endif

< / code>


我想要推送到堆栈的主要部分看起来像这样(部分

无论如何...)


< code>

//创建一份工作

工作* thisJob;

char menuChoice;

int success = 0;


do

{

system(" cls");

Stack< Job * JC;

mainMenu();


cin> menuChoice;

menuChoice = toupper(menuChoice);

cin.ignore(1,''\ n' ');


开关(menuChoice)

{

case''N'':

//分配值

thisJob = new Job;

assignJobValues(thisJob);

success = JC-> Push(& thisJob);


< / code>


我得到的错误是:没有匹配函数来调用

`Stack< Job> ::推(工作**)''
I have been given a class, stack to work with. I have created a class
called Job that I want to push on the stack.

I cannot seem to figure out why the push isn''t working. (I know the
stack works, I have seen it demonstrated.)

The stack header looks like this:

<code>

#ifndef Stackll_h
#define Stackll_h

#include <stdlib.h>

// Stack: Header File

template <class Etype>
class Stack
{
public:

[redacted]

// Push
//
// Input : Element E
// Purpose: To place E on the top of Stack
// Output : 1 if successful; 0 otherwise

int Push ( const Etype & E );

[redacted]
};

#endif
</code>

My main where I am trying to Push onto the stack looks like this (part
of it anyway...)

<code>
//create a job
Job *thisJob;
char menuChoice;
int success = 0;

do
{
system("cls");
Stack<Job*JC;
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, ''\n'');

switch(menuChoice)
{
case ''N'':
//assign values
thisJob = new Job;
assignJobValues(thisJob);
success = JC->Push(&thisJob);

</code>

The error I get is: no matching function for call to
`Stack<Job>::Push(Job**)''



看看模板,看看你在推动什么。


Stack :: Push期待一份工作。而且你传给了一个

指针到一个Job(一个Job **)的地址。


为什么你动态分配你的堆栈?这是Java编程的遗留物,其中一切都必须是新的吗?你正在创造和推动你的工作(好吧,试图推动)同样适用你的工作......

Look at the template, and look at what you''re pushing.

Stack::Push is expecting a Job. And you''re passing it the address of a
pointer to a Job (a Job**).

Why are you dynamically allocating your stack? Is this a remnant of old
Java programming, where everything had to be "new''ed"? Ditto for your
job that you''re creating and pushing (well, trying to push anyways)...




red floyd写道:

red floyd wrote:

>

查看模板,看看你在推动什么。


Stack :: Push期待一份工作。而且你传给了一个

指针到一个Job(一个Job **)的地址。


为什么你动态分配你的堆栈?这是Java编程的遗留物,其中一切都必须是新的吗?你正在创造和推动你的工作是好的b / b
(好吧,试图推动)...
>
Look at the template, and look at what you''re pushing.

Stack::Push is expecting a Job. And you''re passing it the address of a
pointer to a Job (a Job**).

Why are you dynamically allocating your stack? Is this a remnant of old
Java programming, where everything had to be "new''ed"? Ditto for your
job that you''re creating and pushing (well, trying to push anyways)...



嗯,我没有多少控制堆栈,我应该只是

使用它。如果我没有正确使用它,我很想知道我应该怎么做。


至于工作,我可以使用''新的''运营商并获得一个新地址

空间。如果我不使用''new''我不知道怎么一遍又一遍地使用相同的变量

而不会反复使用相同的内存空间。


你知道我的意思吗?


每次循环我需要创建一个新工作然后我将值添加到

(它的属性)然后将它发送到堆栈。


所以我用指针声明我的Job。如果我知道如何告诉系统

来''传递这个指针指向的东西''我想我可以调用

函数。


否则我每次都需要通过

循环来创建一份新工作。


感觉就像一个迷失的学生,我不知道怎么做。

Well, I don''t have much control over the stack, I am supposed to just
use it. If I am not using it correctly I would love to know how I
should be doing it.

As for the Job, I can use the ''new'' operator and get a new address
space. If I don''t use ''new'' I don''t know how to use the same variable
over and over without using the same memory space over and over.

Do you know what I mean?

Each time I loop I need to create a new job which I then add values to
(it''s properties) and then send it off to the stack.

So I declare my Job using a pointer. If I knew how to tell the system
to ''pass the thing this pointer points at'' I think I could call the
function.

Otherwise I need some way to create a new job each time through the
loop.

And feeling rather like a lost student, I don''t know how.


< sa *** @ murdocks.on.cawrote in message

news :11 ********************** @ c28g2000cwb.googlegr oups.com ...
<sa***@murdocks.on.cawrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...

我已经获得了一个类,堆栈可以使用。我创建了一个名为Job的类,我想把它推到堆栈上。


我似乎无法弄清楚为什么推送不起作用。 (我知道

堆栈有效,我已经看过它了。)


堆栈头看起来像这样:
I have been given a class, stack to work with. I have created a class
called Job that I want to push on the stack.

I cannot seem to figure out why the push isn''t working. (I know the
stack works, I have seen it demonstrated.)

The stack header looks like this:



[snip]

[snip]


int Push(const Etype& E);
int Push ( const Etype & E );


do

{

system(" cls");

Stack< Job * JC;
do
{
system("cls");
Stack<Job*JC;



JC指向什么?

What does JC point to?


mainMenu();


cin> menuChoice;

menuChoice = toupper(menuChoice);

cin.ignore(1,''\ n'');


开关(menuChoice)

{

case''N'':

//赋值

thisJob =新工作;
mainMenu();

cin >menuChoice;
menuChoice = toupper(menuChoice);
cin.ignore(1, ''\n'');

switch(menuChoice)
{
case ''N'':
//assign values
thisJob = new Job;



使用''new''创建作业的任何特殊原因?

Any particular reason for using ''new'' to create the Job?


assignJobValues(thisJob );

success = JC-> Push(& thisJob);
assignJobValues(thisJob);
success = JC->Push(&thisJob);



这里你要* thisJob,而不是& thisJob。实际上,& thisJob更好,因为它不会编译。如果你修复了b $ b并运行它,那么程序很可能会崩溃,因为JC还没有被初始化。


你完成它后删除thisJob ?

Here you want *thisJob, not &thisJob. Actually, &thisJob is better because it won''t compile. If you
fix that and run it the program will very likely crash because JC has not been initialized.

Do you delete thisJob when you are finished with it?


>

< / code>


我得到的错误是:没有匹配函数来调用

`Stack< Job> :: Push(Job **)''
>
</code>

The error I get is: no matching function for call to
`Stack<Job>::Push(Job**)''



对。 & thisJob获取thisJob的地址来生成一个Job **。


我建议你让事情变得更容易并停止使用指针,除非你在哪里

真的需要它们。你原本可以:


Stack< JobJC;

....

工作;

JC.Push(工作);


看起来更干净,更简单,没有指针,所以以后什么都不能删除。


DW

Right. &thisJob takes the address of thisJob to produce a Job**.

I suggest that you make things much easier for yourself and stop using pointers except where you
really need them. You could have had:

Stack<JobJC;
....
Job job;
JC.Push(job);

Looks cleaner and simpler, and there are no pointers, so nothing to delete later.

DW


这篇关于学生寻求帮助....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆