By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

trying to get a class to work

P: n/a
I am about to pull my hair out on this one.
its a class stack with push and pop
but one time its trying to find a }
at the end of the code
the next it says that I need a } after private

man I know I am kinda new but this is driving me
up the wall ....
Should I start over or what.
I keep adding things trying to get it to work but no success.

thanks for any and ALL help

Sparks

class Stack
{
public:

StackOP( int = 30 );
void push(float val);
float pop();
int num_items() const;
int full() const;
int empty() const;

private:
int size;
float *top;
float *bottom;
int N=30;

Stack::StackOP() //should this be (int N)????
{
bottom = new float[N];
top = bottom;
size = N;
}
Stack::StackOP()
{
delete [] bottom;
}
Stack::num_items()const
{
return (top-bottom);
}
Stack::push()
{
*top == val;
top++;
}
Stack::pop()
{
top--;
}

};
Feb 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
This should probably work:

class Stack
{
public:

Stack( int = 30 );
void push(float val);
float pop();
int num_items() const;
int full() const;
int empty() const;

private:
int size;
float *top;
float *bottom;
int N=30;
};

Stack::Stack()
{
bottom = new float[N];
top = bottom;
size = N;
}

Stack::~Stack()
{
delete [] bottom;
}

Stack::num_items()const
{
return (top-bottom);
}

Stack::push()
{
*top == val;
top++;
}
Stack::pop()
{
top--;
}

Feb 5 '06 #2

P: n/a
sparks wrote:
class Stack
{
public:
StackOP( int = 30 );
You should use names for parameters, if you don't intend to ignore
them. Also, use symbolic constants (e.g. int const stackSize = 30)
rather than numeric constants.

Also, you're missing a return value. If this is supposed to be a
constructor, it must have the same name as the class.
void push(float val);
Food for thought -- what if you wanted a stack of something besides
floats, and didn't want to rewrite the same code from scratch?
float pop();
In general, it's preferred to have pop() return void, and have a
separate peek() or top() method which provides access to the top
element. The motivation for this comes from the fact that, if you're
returning something by value and the copy constructor fails, there's no
way to recover the stack's original state (the returned item is
irrevocably lost). Of course, in this case you're just returning a
float, so that point is moot. Still, the fundamental lesson is that
separation of concerns is a good thing -- a function with only one
responsibility (e.g. access the top of the stack, or remove the top
element) is easier to manage than one with two (e.g. do both).
int full() const;
The stack can get full? What happens if I call push() when the stack
is full? Also, this should return bool.
int empty() const;
This should return bool.
private:
int size;
float *top;
float *bottom;
Notice that you are storing information redundantly here. Isn't it
true that top always equals bottom+size? If so, why keep the same
information in two places? It's easy for them to get out of synch.
int N = 30;
Should be static and const.
Stack::StackOP() //should this be (int N)????
whoa, whoa, whoa now. This whole section of code is misplaced. All of
these function definitions belong in a separate implementation file,
not in the private section of the class definition. You can provide
definitions inline if you want, but only at the point of declaration,
and not with this syntax.

If you're trying to provide a definition here for the "constructor"
declared earlier, then yes, you should use the same parameter list (a
single int). You shouldn't give it the same name as a private class
member, though.
{
bottom = new float[N];
It bothers me that the stack has dynamically-allocated memory, but
can't grow beyond its initial size.
top = bottom;
size = N;
Which N? What happened to the argument?
}
Stack::StackOP()
Is this supposed to be a destructor? You're missing a couple of
things.
{
delete [] bottom;
}
Stack::num_items()const
{
return (top-bottom);
}
Why not return size? Oh right, you never even use that value. Why
does it exist?
Stack::push()
{
*top == val;
top++;
}
This invokes undefined behavior (usually seg fault) as soon as the
stack overflows.
Stack::pop()
{
top--;
}


This allows top to be less than bottom -- a nonsensical state, likely
to lead to more undefined behavior.

What book are you learning from? I suggest getting exposure to some
more real world examples of working code. Or just writing something
even more basic until you understand the basic paradigms involved.

Luke

Feb 5 '06 #3

P: n/a


Ståle Z. Haugnæss wrote:
This should probably work:

class Stack
{
public:

Stack( int = 30 );
void push(float val);
float pop();
int num_items() const;
int full() const;
int empty() const;

private:
int size;
float *top;
float *bottom;
int N=30;
};

Stack::Stack()
{
bottom = new float[N];
top = bottom;
size = N;
}

Stack::~Stack()
{
delete [] bottom;
}

Stack::num_items()const
{
return (top-bottom);
}

Stack::push()
{
*top == val;
top++;
}
Stack::pop()
{
top--;
}


Is the above code really valid C++? The compiler I'm using gives the
following errors, but I'm not sure how well it matches the standard.

Error E2233 q.cpp 16: Cannot initialize a class member here
Error E2316 q.cpp 20: 'Stack::Stack()' is not a member of 'Stack'
Error E2316 q.cpp 27: 'Stack::~Stack()' is not a member of 'Stack'
Error E2316 q.cpp 37: 'Stack::push()' is not a member of 'Stack'
Error E2316 q.cpp 42: 'Stack::pop()' is not a member of 'Stack'
Warning W8070 q.cpp 44: Function should return a value in function
Stack::pop()

David

Feb 5 '06 #4

P: n/a
David Lindauer wrote:
Ståle Z. Haugnæss wrote:
This should probably work:

class Stack
{
public:

Stack( int = 30 );
void push(float val);
float pop();
int num_items() const;
int full() const;
int empty() const;

private:
int size;
float *top;
float *bottom;
int N=30;
};

Stack::Stack()
{
bottom = new float[N];
top = bottom;
size = N;
}

Stack::~Stack()
{
delete [] bottom;
}

Stack::num_items()const
{
return (top-bottom);
}

Stack::push()
{
*top == val;
top++;
}
Stack::pop()
{
top--;
}
Is the above code really valid C++?


Absolutely not.
The compiler I'm using gives the
following errors, but I'm not sure how well it matches the standard.
I'll address them in turn.
Error E2233 q.cpp 16: Cannot initialize a class member here
Correct. Member data must be initialized in a constructor (in the
body, or (preferably) in the (correctly-ordered) initializer list). I
seem to recall a certain exception to this, but I'll leave that for
someone else to discuss the details of if they care to.
Error E2316 q.cpp 20: 'Stack::Stack()' is not a member of 'Stack'
Correct. The class definition does not declare a nullary
(zero-argument) constructor. It declares a constructor which takes a
single (nameless) integer argument and provides a default value of 30.
Default parameter values do not change the signature of the function --
arguments with defaults must still be part of the argument list in
every declaration, including the declaration which is the definition.
Error E2316 q.cpp 27: 'Stack::~Stack()' is not a member of 'Stack'
Correct. To provide a non-default constructor, the class must
explicitly declare the destructor in its definition. Otherwise, how
would other code know of its existence?
Error E2316 q.cpp 37: 'Stack::push()' is not a member of 'Stack'
Correct. The signatures do not match, and the return type is missing
(as it is for the other definitions.
Error E2316 q.cpp 42: 'Stack::pop()' is not a member of 'Stack'
Correct. Same problem.
Warning W8070 q.cpp 44: Function should return a value in function
Stack::pop()


pop() must either return a float, or change its return type to void.

Compilers are readily available for free anywhere there's an internet
connection, and can help avoid embarassing mistakes. The above error
messages are far from cryptic, and paying attention to them helps you
gain a closer understanding of how the language, and the compiler,
work.

Luke

Feb 5 '06 #5

P: n/a


Luke Meyers wrote:
David Lindauer wrote:
<snip>
Is the above code really valid C++?


Absolutely not.
The compiler I'm using gives the
following errors, but I'm not sure how well it matches the standard.


I'll address them in turn.
Error E2233 q.cpp 16: Cannot initialize a class member here


Correct. Member data must be initialized in a constructor (in the
body, or (preferably) in the (correctly-ordered) initializer list). I
seem to recall a certain exception to this, but I'll leave that for
someone else to discuss the details of if they care to.
Error E2316 q.cpp 20: 'Stack::Stack()' is not a member of 'Stack'


Correct. The class definition does not declare a nullary
(zero-argument) constructor. It declares a constructor which takes a
single (nameless) integer argument and provides a default value of 30.
Default parameter values do not change the signature of the function --
arguments with defaults must still be part of the argument list in
every declaration, including the declaration which is the definition.
Error E2316 q.cpp 27: 'Stack::~Stack()' is not a member of 'Stack'


Correct. To provide a non-default constructor, the class must
explicitly declare the destructor in its definition. Otherwise, how
would other code know of its existence?
Error E2316 q.cpp 37: 'Stack::push()' is not a member of 'Stack'


Correct. The signatures do not match, and the return type is missing
(as it is for the other definitions.
Error E2316 q.cpp 42: 'Stack::pop()' is not a member of 'Stack'


Correct. Same problem.
Warning W8070 q.cpp 44: Function should return a value in function
Stack::pop()


pop() must either return a float, or change its return type to void.

Compilers are readily available for free anywhere there's an internet
connection, and can help avoid embarassing mistakes. The above error
messages are far from cryptic, and paying attention to them helps you
gain a closer understanding of how the language, and the compiler,
work.

Luke


Thanks for taking the time to answer :)

David

Feb 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.