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

Home Posts Topics Members FAQ

Compile time error

2 New Member
Hi, could anyone please tell me what is the problem with the following code :

const int MAX = 100;

class Stack{
protected:
int stk[MAX];
int top;
public:
Stack()
{ top = 0; }
void push(int var)
{ cout<<"Top = "<<top<<endl;
stk[++top] = var;
cout<<"Top after = "<<top<<endl;
}
int pop()
{ cout<<"Top POP = "<<top<<endl;
return stk[top--]; }
};
class Stack2: public Stack
{
public:
void push(int var)
{
//Stack s;
if (top != MAX){
cout<<"Stack2 push"<<endl;
void Stack::push(int var);
}
else
cout<<"Stack is full"<<endl;
}
int pop()
{
if ( top > 1)
return Stack::pop();
else
cout<<"Nothing to pop"<<endl;
}
};

int main(int argc, char *argv[])
{
Stack2 s1;
cout<<"In the Program"<<endl;
s1.push(11);
s1.push(22);
s1.push(33);

cout<<"S 1 = "<<s1.pop()<<endl;
cout<<"S 2 = "<<s1.pop()<<endl;
cout<<"S 3 = "<<s1.pop()<<endl;
cout<<"S 4 = "<<s1.pop()<<endl;

getchar();
return 0;
}

The error is :
cannot declare member function 'Stack::push' within Stack2

Thanks
Mar 26 '08 #1
3 1637
weaknessforcats
9,208 Recognized Expert Moderator Expert
quote=shrew83]
class Stack2: public Stack
{
public:
void push(int var)
{
//Stack s;
if (top != MAX){
cout<<"Stack2 push"<<endl;
void Stack::push(int var); <<<<<<<<<<<<<<<<<<<<<<<
}
etc...
[/code]

The <<<<<<< line is a function prototype. It redefines the one in the Stack class. Redefinitions are not allowed in C++.
Mar 26 '08 #2
shrew83
2 New Member
Ok then how do I implement the scenario wherein I want to call the the push function of Stack from the push function of Stack2.
Mar 26 '08 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Stack2 inherits from Stack. That means it inherits the push function. Just call push() using your Stack2 object and the call will be made to Stack::push().

Remember, a derived object, like a Stack2 object, has an embedded base object (Stack) inside. In reality, the Stack2 call to the Stack::push() will push the data that's inside the base portioin of the Stack2 object.

There is no need for a push method in Stack2.
Mar 26 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
5
by: Brice Prunier | last post by:
Here under 4 schemas i'm working with ( it may be long: sorry...) The context is the following : Resident.xsd imports Person.xsd and includes Common.xsd ( anonimous schema: no TargetNamespace )...
2
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a...
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
0
by: ufnuceda | last post by:
Hello everyone, I was wondering if any of you have some experience with the boost library. I am having trouble compiling code with it. Since boost is being used a lot these days I thought some...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
15
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
9
by: ThunderMusic | last post by:
Hi, I'd like to create a compile time error in my class... maybe there's a way already built in in the framework so I can achieve what I want... I have 2 constructors in my class. One of them...
12
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.