Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 565119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:50:18+00:00 2026-05-13T12:50:18+00:00

I created the following two C++ files: Stack.cpp #include<iostream> using namespace std; const int

  • 0

I created the following two C++ files:

Stack.cpp

#include<iostream>

using namespace std;

const int MaxStack = 10000;
const char EmptyFlag = '\0';

class Stack {

    char items[MaxStack];
    int top;
public:
    enum { FullStack = MaxStack, EmptyStack = -1 };
    enum { False = 0, True = 1};
    // methods
    void init();
    void push(char);
    char pop();
    int empty();
    int full();
    void dump_stack();
};

void Stack::init()
{
    top = EmptyStack;
}

void Stack::push(char c)
{
    if (full())
        return;

    items[++top] = c;
}

char Stack::pop()
{
    if (empty())
        return EmptyFlag;
    else
        return items[top--];
}

int Stack::full()
{
    if (top + 1 == FullStack)
    {
        cerr << "Stack full at " << MaxStack << endl;
        return true;
    }
    else
        return false;
}

int Stack::empty()
{
    if (top == EmptyStack)
    {
        cerr << "Stack Empty" << endl;
        return True;
    }
    else
        return False;
}

void Stack::dump_stack()
{
    for (int i = top; i >= 0; i--)
    {
        cout << items[i] << endl;
    }
}

and StackTest.cpp

#include <iostream>

using namespace std;

int main()
{

Stack s;
s.init();

s.push('a');
s.push('b');
s.push('c');

cout << s.pop();
cout << s.pop();
cout << s.pop();

}

Then I try to compile with:

[USER@localhost cs3110]$ g++ StackTest.cpp Stack.cpp
StackTest.cpp: In function int main()':
StackTest.cpp:8: error:
Stack’ was not declared in this scope
StackTest.cpp:8: error: expected ;' before "s"
StackTest.cpp:9: error:
s’ was not declared in this scope

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:50:18+00:00Added an answer on May 13, 2026 at 12:50 pm

    Your Stack is declared in Stack.cpp, as you said. You are trying to use it in StackTest.cpp. Your Stack is not declared in StackTest.cpp. You can’t use it there. This is what the compiler is telling you.

    You have to define classes in all translation units (.cpp files), in which you are planning to be using them. Moreover, you have to define them identically in all of these translation units. In order to satisfy that requirement, class definitions are usually separated into header files (.h files) and included (using #include) into each .cpp file where they are needed.

    In your case you need to create header file Stack.h, containing the definition of Stack class (and constant definitions in this case) and nothing else

    const int MaxStack = 10000; 
    const char EmptyFlag = '\0'; 
    
    class Stack { 
    
        char items[MaxStack]; 
        int top; 
    public: 
        enum { FullStack = MaxStack, EmptyStack = -1 }; 
        enum { False = 0, True = 1}; 
        // methods 
        void init(); 
        void push(char); 
        char pop(); 
        int empty(); 
        int full(); 
        void dump_stack(); 
    }; 
    

    (Header files also benefint from use of so called include guards, but it will work as shown above for now).

    This class definition should be moved from Stack.cpp to Stack.h. Instead you will include this .h file into Stack.cpp. Your Stack.cpp will begin as follows

    #include<iostream>     
    
    #include "Stack.h"
    
    using namespace std;    
    
    void Stack::init()      
    {      
        top = EmptyStack;      
    }      
    
    // and so on...
    

    The rest of your former Stack.cpp, i.e. member definitions, should remain as is.

    The Stack.h should also be included into StackTest.cpp, in the same way, so your StackTest.cpp should begin as

    #include <iostream>        
    
    #include "Stack.h"
    
    using namespace std; 
    
    // and so on...
    

    That’s, basically, it. (And instead of providing an init method a better idea would be to create a constructor for the Stack class. But that’s a different story).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an alert view with two buttons using the following code: UIAlertView
I've created a small python script to toggle between two files I'm using for
I have created following thing in android using android compatibility support package Basically i
I required to share a const value between two files; so rather than keeping
I have created a virtual file system(very simular to fat) based on two files.
In my Firefox add-on using the Add-on SDK, I'm trying to include two library
What is the difference in the following two CREATE TABLE statements? (The first one
Look at the following two ways to create a new object of class Y:
I want to create a view from the following two tables: Entry: entry_id entry_date
How can I create an anonymous and curried function in Scala? The following two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.