Method for solving error: "cannot instantiate abstract class" - c++

I find one of the most time-consuming compiler errors for me is "cannot instantiate abstract class," since the problem is always that I didn't intend for the class to be abstract and the compiler doesn't list which functions are abstract. There's got to be a more intelligent way to solve these than reading the headers 10 times until I finally notice a missing "const" somewhere. How do you solve these?

cannot instantiate abstract class
Based on this error, my guess is that you are using Visual Studio (since that's what Visual C++ says when you try to instantiate an abstract class).
Look at the Visual Studio Output window (View => Output); the output should include a statement after the error stating:
stubby.cpp(10) : error C2259: 'bar' : cannot instantiate abstract class
due to following members:
'void foo::x(void) const' : is abstract
stubby.cpp(2) : see declaration of 'foo::x'
(That is the error given for bdonlan's example code)
In Visual Studio, the "Error List" window only displays the first line of an error message.

C++ tells you exactly which functions are abstract, and where they are declared:
class foo {
virtual void x() const = 0;
};
class bar : public foo {
virtual void x() { }
};
void test() {
new bar;
}
test.cpp: In function ‘void test()’:
test.cpp:10: error: cannot allocate an object of abstract type ‘bar’
test.cpp:5: note: because the following virtual functions are pure within ‘bar’:
test.cpp:2: note: virtual void foo::x() const
So perhaps try compiling your code with C++, or specify your compiler so others can give useful suggestions for your specific compiler.

C++Builder tells you which method is abstract:
class foo {
virtual void x() const = 0;
};
class bar : public foo {
virtual void x() { }
};
new bar;
[BCC32 Error] File55.cpp(20): E2352 Cannot create instance of abstract class 'bar'
[BCC32 Error] File55.cpp(20): E2353 Class 'bar' is abstract because of 'foo::x() const = 0'

Related

On Compilation The C++ Project throws Error C2228 which is not expected as the Control does not reaches the point at runtime

Base Class Contains Public Function Bar().
foo() is a generic function which takes one parameter
When I pass Object of derived class it works fine but when I pass built in data type it must throw exception but it is showing compile time error C2228 In Visual Studio.
class Base
{
public:
void Bar();
};
class Derived:Base
{};
template<typename T>
void foo(T object)
{
if(std::is_base_of<Base,T>::value == true)
object.Bar();//Control does not reaches the point
else
throw "Invalid Parameter";
}
int main()
{
Derived objDerived;
foo(objDerived); //WORKS GOOD
foo(2);//ERROR C2228
}
What you want is c++17 if constexpr instead. Otherwise, you will try to instantiate the branch which leads to the error. This is available in vs2017.
In c++11, you need two functions, not one, and use enable_if instead.

g++4.9 not finding method overload in derived class when compiling s-function in matlab

I'm trying to make the following implementation work:
Header of base class:
#ifndef BASE_HH
#define BASE_HH
namespace base {
class Agent
{
public:
int loop () {return loopImpl();}
protected:
virtual int loopImpl() =0;
}
#endif
Header or derived class:
#ifndef DERIVED_HH
#define DERIVED_HH
#include <base/agent.hh>
namespace derived {
class Agent : public base::Agent
{
protected:
virtual int loopImpl();
}
#endif
Source file of derived class:
#include <derived/agent.hh>
int Agent::loopImpl()
{
return 0;
}
Now, when I compile this in the terminal and test it, it works. But when I try compiling an S-function in Matlab that creates an Agent object, i get the following error:
Error using mex
/path/sfunction.cpp In function ‘void mdlStart(SimStruct*)’:
/path/sfunction.cpp:51:56: error: invalid new-expression of abstract class type ‘derived::Agent’
derived::Agent *agent = new derived::Agent ();
/derived_path/agent.hh:28:11: note: because the following virtual functions are pure within ‘derived::Agent’:
class Agent: public base::Agent
In file included from /derived_path/agent.hh:22:0, from /path/sfunction.cpp:13:
/base_path/agent.hh:54:21: note: virtual int base::Agent::loopImpl() virtual int loopImpl () =0;
So it seems the g++4.9 compiler is unable to find the derived member function... Could anybody give me some hints on why this is so and what to do about it? As mentioned above, when I compile a similar file creating an object of the same derived class, it works.
Thank you for your time.

Code Compilation Fails on Linux, Succeeds on Windows: Cause/Fix?

I have some c++ code that will compile fine in Visual Studio 2013 but will not compile in linux using g++ (no IDE).
What is causing the difference and how can I make the code compile on linux? Is it because they are different compilers? Do I need a specific compiler setting?
Code:
#include <iostream>
typedef class IApp;
typedef class Component;
class Component
{
public:
protected:
IApp* app;
template<typename T>
void registerEvent()
{
app->logEvent();
}
};
class IApp : protected Component
{
public:
static IApp NULL_APP;
void logEvent()
{
printf("Event Logged\n");
}
protected:
virtual void foo() = 0;
};
int main(int argc, char** argv)
{
printf("Alive\n");
system("pause");
return 0;
}
On windows I get no compiler errors. On linux I get the following compiler errors:
g++ - o res main.cpp - std = c++11
main.cpp:3 : 15 : warning : ‘typedef’ was ignored in this declaration[enabled by default]
typedef class IApp;
^
main.cpp:4 : 15 : warning : ‘typedef’ was ignored in this declaration[enabled by default]
typedef class Component;
^
main.cpp: In member function ‘void Component::registerEvent()’ :
main.cpp : 16 : 6 : error : invalid use of incomplete type ‘class IApp’
app->logEvent();
^
main.cpp:3 : 15 : error : forward declaration of ‘class IApp’
typedef class IApp;
^
main.cpp: At global scope :
main.cpp : 23 : 14 : error : cannot declare variable ‘IApp::NULL_APP’ to be of abstract type ‘IApp’
static IApp NULL_APP;
^
main.cpp:20 : 7 : note : because the following virtual functions are pure within ‘IApp’ :
class IApp : protected Component
^
main.cpp : 31 : 15 : note : virtual void IApp::foo()
virtual void foo() = 0;
^
make: ***[all] Error 1
Here you should simply remove typedef:
typedef class IApp;
Then this template method should be defined out-of-line, below IApp:
template<typename T>
void registerEvent()
{
app->logEvent();
}
Otherwise, it doesn't see the declaration of IApp which it needs to dereference.
Finally, this does not make sense:
virtual void foo() = 0;
Because the same class has a static member of its own class type, so needs to instantiate it. But you've prevented that with a pure virtual function.
GCC is right not to compile this code.
Leave out the typedef keywords before the forward class declarations, they're not necessary. Just class MyClass; is sufficient for a forward declaration. The other issue is that foo() is pure virtual - you need to implement it if you want to instantiate the class. I'm surprised the Microsoft complier doesn't complain but then that's Microsoft.

C++: Compiler error overriding function from DLL

I am attempting to override an abstract class that has been declared inside a DLL in another project, however, when I try to init the overriding class, I receive compilation errors.
These are visual studio projects, as such,the built-in compiler of visual studio are used to compile all code.
I have a template class in a DLL:
#ifndef __IINPUT_RECEIVER_H_
#define __IINPUT_RECEIVER_H_
#ifdef HUMANINTERACTION_EXPORTS
#define HUMANINTERACTION_API __declspec(dllexport)
#else
#define HUMANINTERACTION_API __declspec(dllimport)
#endif
namespace HumanInteraction
{
template<typename T>
class HUMANINTERACTION_API IInputReceiver
{
public:
virtual ~IInputReceiver()
{}
/**
* Called when new input is received
*/
virtual void onInput(const T& refInput) = 0;
};
}
#endif
In another project which is to use this DLL, I inherit from this class and override its method, like so:
class Receiver : public IInputReceiver<wchar_t*>
{
public:
/**
* Called when new input is received
*/
virtual void onInput(const wchar_t*& refInput)
{
wstring str(refInput);
wcout << L"Received: " << str << endl;
}
};
But when I try to init an instance of class Receiver I receive the following compilation error; I am omitting unrelated logs:
1>c:\users\dominik\documents\visual studio 2013\projects\profile\profile\profile.cpp(25): error C2259: 'Receiver' : cannot instantiate abstract class
1> due to following members: 1> 'void
HumanInteraction::IInputReceiver::onInput(const T &)' : is abstract
The line profile.cpp(25) attempts to init an instance of Receiver, like so - nothing special here:
Receiver receiver;
Of course the compiler is right that onInput in the base class in a pure virtual function, however, I override this function in my derived class. Therefore, the class Receiver is not abstract as the log indicates.
Though usually I tend to fix my issues on my own, I have absolutely no clue whatsoever this time why this error is occuring - absolutely void.
I would greatly appreciate any contribution to this problem, even if it is 'only' comments.
The signature of the virtual member function in the base class is
virtual void onInput(const T& refInput) = 0;
The one in the derived class is
virtual void onInput(const wchar_t*& refInput)
What you need to use is:
virtual void onInput(wchar_t* const& refInput)
It will make more sense if you use
virtual void onInput(T const& refInput) = 0;
in the base class.

gcc compiling error: member of nested class A in template class Table is not visible in nested friend class. Why?

I am trying to compile some code in mingw32 on windows XP and I got an error. So, I have write a simplified version of that code and got same error.
Here it is:
template <class T>
class Table
{
public:
class A
{
private:
int nEntry;
friend class B;
};
class B : public A
{
public:
void Remove()
{
nEntry = 1;
}
};
};
Compiler error message:
E:\cbProjects\projects\1\main.cpp||In member function 'void Table<T>::B::Remove()':|
E:\cbProjects\projects\1\main.cpp|24|error: 'nEntry' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|
Where I can read the items why it's so? (links will be usefull - it may be happen that some other similar errors or compiler bags can appear)
Within a template, member access must sometimes be preceded by an explicit this->, as in this case. You can make your code work this way:
this->nEntry = 1;