473,473 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

trying to get a class to work

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
5 1727
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
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


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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: EasyRider41 | last post by:
I am trying to merge to scripting samples I for on a source code web site and having limited luck. Then first one is called Zebra Tables witch colors alternate rows of a table to look beter. The...
2
by: Gill Bates | last post by:
I'm trying to login to a banking site (https://www.providentconnection.com) using vb.net. I've tried many variations of WebClient and HttpWebRequest; none of which I've got to work. My latest...
1
by: sapropel | last post by:
ive been trying to understand Inheritance, Polymorphism and templates, and so ive tried to make a test using all of those. it is about a base class taht receives two params on the template and two...
1
by: tuko | last post by:
Hello kind people. I have the classes "point", "curve", "surface", "region" as shown below. Every class has a member function that returns a list of pointers to objects that comprise the *this...
13
by: cj | last post by:
Stephany Young provided me with the following code to count threads created by my program. Public Class MyThreadCount Private Shared m_lock As New Object Private Shared m_threadcount As Int32...
5
by: timothy.pollard | last post by:
Hi I'm having a bit of bother trying to make a questionnaire do what I want it to. I have put it up on www.web-iq.co.uk/test.htm. Basically the user of the final form (when I've tarted it up)...
6
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in...
3
by: tshad | last post by:
I have dataGrid that I am filling from a List Collection and need to sort it by the various columns. So I need to be able to sort the Collection and found that you have to set up your own...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.