For a project I'm doing for class I have to make a virtual method in a class. A subclass will inherit the virtual method and instantiate it in a separate .cpp file. The problem I'm having is that I need the class that has the virtual method to instantiate its version of the method as well as the subclass. I get an error that says "cannot instantiate an abstract class" even though it is instantiated in a .cpp.

Here's my code:

Subdomain.h

#pragma once

#include "String.h"
using namespace std;

class SubdomainPart
{
public:
	SubdomainPart() {}
	// Sets the Address
	SubdomainPart(const String& address);
	virtual bool IsValid() = 0;

private:
	String Address;
};

Subdomain.cpp

#include "SubdomainPart.h"
#include "Validation.h"
#include "Definitions.h"
#include "String.h"

SubdomainPart::SubdomainPart(const String& address)
{
	Address = address;
}

// Implements subdomain validation rules
bool SubdomainPart::IsValid()
{
	// 1) Check size
	if(Address.GetLength() > MAX_DOMAIN_SIZE)
		return false;
	// 2) Check for valid characters
	int check = Address.Find_First_Not_Of(VALID_LOCALPART_CHARS);
	if(check != String::npos)
		return false;

	// 3) Check dot/dash rule
	if(!DotDashRules(Address))
		return false;

	return true;
}

Subclass.h:

#pragma once
#include "SubdomainPart.h"
#include "String.h"
#include "Vector.h"
using namespace std;

class TldPart : public SubdomainPart
{
public:
	TldPart() {}
	void Set(String &source);
	static void PreloadTlds();
	static Vector<String> Tlds;
	bool IsValid();

private:
	String Address;
};

Subclass.cpp:

#pragma once
#include "SubdomainPart.h"
#include "String.h"
#include "Vector.h"
using namespace std;
bool TldPart::IsValid()
{
	String newTld;
	for(int i=0; i<Tlds.GetSize(); i++)
	{
		//Forces uppercase
		toLower(Tlds[i]);
		if(Tlds[i] != Address)
			return false;
	}
	return true;
}

>> I get an error that says "cannot instantiate an abstract class" even though it is instantiated in a .cpp.

Classes that contain pure virtual functions can not be instantiated o their own -- inherited class must implement the function and the inherited class must be instantiated.

SubdomainPart is not instantiated in any *.cpp file that you posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.