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 4172548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:34:34+00:00 2026-05-21T00:34:34+00:00

again this question is also origined from Thinking in C++ Chapter7, Q#7. I believe

  • 0

again this question is also origined from “Thinking in C++” Chapter7, Q#7. I believe the Stack header file should be changed to Stack.h

        #ifndef STACK_H
        #define STACK_H

        class Stack {
          struct Link {
            void* data;
            Link* next;
            Link(void* dat, Link* nxt);
            ~Link();
          }* head;
        public:
          Stack();
          Stack(void* arr[], int size);
          ~Stack();
          void push(void* dat);
          void* peek();
          void* pop();
        };

and the implementation of Stack::Stack(void* arr[], int size) in Stack.cpp, I believe could be like:

       Stack::Stack(void* arr[], int size)
       {
         for (int i=0; i<size; i++)
          {
            push(arr[i]);
          }
       }

However, in the main test file StackTest.cpp, how could I pass the address of a string array to this constructor? Here is what I come up with:

          #include "Stack.h"
          #include "require.h"
          #include <iostream>
          #include <string>
          using namespace std;

          int main() {

            string tst_arr[] = {"hi 1", "hi 2", "hi 3"};
            Stack string_arr((void**)tst_arr, 3);
            string* s;
            while((s = (string*)string_arr.pop()) != 0) {
              cout << *s << endl;
              delete s;
            }
          } 

But it has some segmentation fault. What I could think of is to change Stack::Stack(void* arr[], int size) to Stack::Stack(string arr[], int size), however it doesn’t satisfies the question requirement. The purpose of Stack to store generic objects, including string for example. I believe I still have difficulty to understand the conceipt of void* pointer and array of pointers and the chagne between string array to void* array etc… Anyone could help me solve this problem? Thanks a lot!!

  • 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-21T00:34:35+00:00Added an answer on May 21, 2026 at 12:34 am

    Your Stack constructor asks for an array of pointers on stuffs, and you give it an array of objects. As a bonus, I give you proper main function and freeing memory ^^

    #include <cstdlib>
    // --- Includes your stuffs ---
    
    using namespace std;
    
    int main(int argc, char* argv[]) {
      string* tst_arr[] = new string[3];
      tst_arr[0] = new string("hi 1");
      tst_arr[1] = new string("hi 2");
      tst_arr[2] = new string("hi 3");
      Stack string_arr((void**)tst_arr, 3);
    
      // --- Do your stuffs ---
    
      for(int i =0; i < 3; ++i)
        delete tst_arr[i];
      delete[] tst_arr;
    
      return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I believe this question is related to this one also, since I'm trying to
I saw this question and it motivated me to look again (without success) at
Again I have a question regarding this plugin: http://t.wits.sg/2008/06/20/jquery-progress-bar-11/ What I want to achieve
This is my 3rd SO question. Please help again... I want to put up
I tried posting this question earlier but was unsuccessful so I'm trying again I
Long-delayed update I'm accepting MUG4N's answer to this question, and I also want to
ADDED: This question is now, I believe, subsumed by this one: Using GNU Screen
So earlier I asked this question: JQuery Login Redirect. Code Included The php file
Hi and thank you again for looking into this question. I have one mysql
Once again I'm asking this question, and still can't find any reasonable solution. I

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.