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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:04:53+00:00 2026-05-11T15:04:53+00:00

I’m trying to use an abstract class when passing an extended object as an

  • 0

I’m trying to use an abstract class when passing an extended object as an parameter to a function, but my attempts so far have led to some compiler errors.

I have a few clues as to what the problem is, I’m obviously not allowed to instantiate an abstract class, and I believe some of the code in MyClass is trying to do this, even though this is not my intention. Some researching has suggested that I should reference the object as a pointer to achieve what I want, but my attempts so far have failed and I’m not even sure that this is the answer (hence my asking here).

I’ll submit now that I’m more familiar with Java than C++, and I’m sure part of my problem is due to this.

Here is an example of what I’m trying to do in my program:

class A {     public:         virtual void action() = 0; };  class B : public A {     public:         B() {}          void action() {             // Do stuff         } };  class MyClass {      public:          void setInstance(A newInstance) {             instance = newInstance;         }          void doSomething() {             instance.action();         }      private:          A instance; };  int main(int argc, char** argv) {     MyClass c;     B myInstance;     c.setInstance(myInstance);     c.doSomething();     return 0; } 

This example produces the same compiler error that I am getting in my program:

sean@SEAN-PC:~/Desktop$ gcc -o test test.cpp test.cpp:20: error: cannot declare parameter ‘newInstance’ to be of abstract type ‘A’ test.cpp:2: note:   because the following virtual functions are pure within ‘A’: test.cpp:4: note:   virtual void A::action() test.cpp:30: error: cannot declare field ‘MyClass::instance’ to be of abstract type ‘A’ test.cpp:2: note:   since type ‘A’ has pure virtual functions test.cpp: In function ‘int main(int, char**)’: test.cpp:36: error: cannot allocate an object of abstract type ‘A’ test.cpp:2: note:   since type ‘A’ has pure virtual functions 

Update

Thanks for the feedback everyone.

I’ve since changed ‘MyClass::instance to contain a pointer of type A, but I now get some bizarre errors related to the vtable:

sean@SEAN-PC:~/Desktop$ gcc -o test test.cpp /tmp/ccoEdRxq.o:(.rodata._ZTI1B[typeinfo for B]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info' /tmp/ccoEdRxq.o:(.rodata._ZTI1A[typeinfo for A]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info' /tmp/ccoEdRxq.o:(.rodata._ZTV1A[vtable for A]+0x8): undefined reference to `__cxa_pure_virtual' collect2: ld returned 1 exit status 

My modified code is as follows (A and B have not been modified):

class MyClass {      public:          void setInstance(A* newInstance) {             instance = newInstance;         }          void doSomething() {             instance->action();         }      private:          A* instance; };  int main(int argc, char** argv) {     MyClass c;     B myInstance;     c.setInstance(&myInstance);     c.doSomething();     return 0; } 
  • 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. 2026-05-11T15:04:54+00:00Added an answer on May 11, 2026 at 3:04 pm

    Your problem is that you should accept a reference in your function. The reason is that a reference does not actually copy the argument passed. If you however accept an A – instead of a reference A& – then you actually copy the argument passed into the parameter object, and what you get is an object of type A – but which is actually not allowed!

        // the reference parameter will reference the actual argument     void setInstance(A &newInstance) {             // assign the address of the argument to the pointer member             // instance.              instance = &newInstance;     } 

    And then you will have to change the member in your class to be a pointer. It can’t be a reference because setInstance will change what it references – a reference can only reference one object during its entire lifetime, while a pointer can be set to point do different things just by reassigning it a different address. The remaining parts look like this then

        void doSomething() {         // call a member function on the object pointed to         // by instance!         instance->action();     }  private:      // a pointer to some object derived from A     A *instance; 

    Also note that you have to compile C++ programs using g++, because it additionally links the C++ standard library to your code

    g++ -o test test.cpp # instead of gcc! 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 148k
  • Answers 148k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer var jsonData = from user in users select new[] {… May 12, 2026 at 9:22 am
  • Editorial Team
    Editorial Team added an answer With your query in design view, click in your field… May 12, 2026 at 9:22 am
  • Editorial Team
    Editorial Team added an answer It means there's a mandatory property has been assigned nil.… May 12, 2026 at 9:22 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.