I made 2 managed classes in C++ clr, and I have a constructor on the base class. The derived class is inheriting from the base one, and I believe that since my constructor on the parent class has parameters not equal to void, that when I call it to be inherited it needs a parameter for the constructor. I believe this because when I stop inheriting, the error goes away. I can't just put (parameter) after inheriting it, so my question is how do I get it to be inherited without asking for a parameter for the constructor.
Here is my full error:
Severity Code Description Project File Line Suppression State
Error C2512 'ConnectDB': no appropriate default constructor available Credit Card Storage System C:\Users\dehla\Desktop\Visual Studio Projects\C++\Credit Card Validator\Query.h 26
Here is my Header File:
#ifndef DATA_BASE
#define DATA_BASE
using namespace System;
using namespace System::Data::SqlClient;
ref class ConnectDB{
protected:
SqlConnection^ cnn;
String^ sql_file;
bool state;
public:
ConnectDB(System::String^ in_file);
bool ConnectDataBase();
~ConnectDB(void);
};
ref class Query : public ConnectDB{
private:
public:
};
#endif
Here is my cpp file:
#include "Query.h"
ConnectDB::ConnectDB(System::String^ in_file){
sql_file = System::IO::File::ReadAllText(in_file);
}
bool ConnectDB::ConnectDataBase() {
String^ connectionString = "Data Source=WIN-50GP30FGO75;Initial Catalog=Demodb;User ID=sa;Password=demol23";
SqlConnection^ cnn = gcnew SqlConnection(connectionString);
cnn->Open();
state = true;
return true;
}
ConnectDB::~ConnectDB() {
cnn->Close();
}
Your subclass doesn't need a constructor with arguments, but all constructors need to call the base class constructor properly.
As the base class doesn't have a default constructor, you need to do something like this:
class Query : public ConnectDB{
private:
public:
Query(): ConnectDB("") { ... }
};
The thing is, unless you want to hardcode the value input to ConnectDB
, your base class probably needs a constructor with a string parameter so you can forward it to ConnectDB's
constructor.