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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:07:42+00:00 2026-05-12T18:07:42+00:00

I have to do this for a basic C++ lecture at my university, so

  • 0

I have to do this for a basic C++ lecture at my university, so just to be clear: i would have used the STL if i was allowed to.

The Problem: I have a class named “shape3d” from which i derived the classes “cube” and “sphere”. Now i have to implement “shape3d_stack”, which is meant be able of holding objects of the types “cube” and “sphere”. I used arrays for this and it worked quite well when i tried to do so with a stack of ints. I tried to do it like so:

shape3d_stack.cpp:

15    // more stuff
16    
17        shape3d_stack::shape3d_stack (unsigned size) :
18         array_ (NULL),
19         count_ (0),
20         size_  (size)
21        { array_ = new shape3d[size]; }
22    
23    // more stuff

but, unfortunately, the compiler tells me:

g++ -Wall -O2 -pedantic -I../../UnitTest++/src/ -c shape3d_stack.cpp -o shape3d_stack.o
shape3d_stack.cpp: In constructor ‘shape3d_stack::shape3d_stack(unsigned int)’:
shape3d_stack.cpp:21: error: cannot allocate an object of abstract type ‘shape3d’
shape3d.hpp:10: note:   because the following virtual functions are pure within ‘shape3d’:
shape3d.hpp:16: note:  virtual double shape3d::area() const
shape3d.hpp:17: note:  virtual double shape3d::volume() const

i guess this must be some kind of really ugly design error caused by myself. so how would be the correct way of using all kinds of objects derived from “shape3d” with my stack?

  • 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-12T18:07:43+00:00Added an answer on May 12, 2026 at 6:07 pm

    You can’t create objects from abstract classes.
    You’ll probably want to create an array of pointers to the abstract class, which is allowed, and fill them with derived instances:

    // declaration somewhere:
    shape3d** array_;
    
    // initalization later:
    array_ = new shape3d*[size];
    
    // fill later, triangle is derived from shape3d:
    array_[0] = new triangle;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
I have a basic website nav layout that looks like this: <li class=folder parent_folder>
I know this sounds confusing: I have just built some basic prev/next pagination for
I have this basic setup: enum{ BASE, PRIMITIVE, ... }; class IUnknown{ public: bool
I have this very basic problem, >>> from django.core import serializers >>> serializers.serialize(json, {'a':1})
I have this code for my very very basic WPF Project. <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
I have this basic utility class fetches (possibly) shortened URLs in parallel and returns
I have this basic HTML structure: <fieldset> <input/> <label></label> <div class=toggle_box> <div class=switch></div> </div>
i have this basic html structure: <div class=a> <div class=m></div> <div class=m></div> </div> <div
hi i have this really basic class here .. and i want to use

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.