Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing an abstract class in C++
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>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.</p> <p>Here is an example of what I'm trying to do in my program:</p> <pre><code>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; } </code></pre> <p>This example produces the same compiler error that I am getting in my program:</p> <pre><code>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 </code></pre> <h2>Update</h2> <p>Thanks for the feedback everyone.</p> <p>I've since changed "MyClass::instance to contain a pointer of type A, but I now get some bizarre errors related to the vtable:</p> <pre><code>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 </code></pre> <p>My modified code is as follows (A and B have not been modified):</p> <pre><code>class MyClass { public: void setInstance(A* newInstance) { instance = newInstance; } void doSomething() { instance-&gt;action(); } private: A* instance; }; int main(int argc, char** argv) { MyClass c; B myInstance; c.setInstance(&amp;myInstance); c.doSomething(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload