Base Class Pointer pointing to a Derived Class Object which is a template variable in another class
Sorry if the title is not clear.But, I'll try to explain the problem clearly.
I am working on a c++ and QT using MVC for the application architecture:
- Some description for application structure:
Model Section (designed using polymorphism pattern):
is responsible for database related stuff, It has an IDataBase.h interface and its implementation for sqlite database type.
IDataBase.h :
namespace model{
class IDataBase;
}
class IDataBase
{
public:
virtual ~IDataBase(){
//;
}
virtual bool connect(const QString &db) = 0;
virtual bool insertData(QMap<QString,QVariant> &queryData, const
QString &table) = 0;
};
sqlitedb.h
class SqliteDB: public IDataBase
{
public:
SqliteDB();
~SqliteDB();
bool connect(const QString &db)
bool insertData(QMap<QString,QVariant> &queryData, const QString &table);
private:
QSqlDatabase m_database;
};
Controller Section: I am using the template class DataBaseService to manage any type of databases (mysql, sqlite ....).
DataBaseService.h
// template variable is the database type class
template<class T>
class DatabaseService
{
public:
DatabaseService();
~DatabaseService();
bool addProduct(QMap<QString,QVariant> &queryData, const QString &table);
private:
std::unique_ptr<IDataBase> m_database;
};
DataBaseService.cpp
template<class T>
DatabaseService<T>::DatabaseService()
{
m_database = std::make_unique<T>();
m_database->connect("db.sqlite");
}
- The Problem is :
When I try to create a pointer of class DatabaseService with the template variable SqliteDB in main.cpp file :
DatabaseService<model::SqliteDB> *sqliteDb = new DatabaseService<model::SqliteDB>();
I get This unresolved externals error :
error: LNK2019: unresolved external symbol "public: __cdecl
DatabaseService::DatabaseService(void)"
(??0?$DatabaseService@VSqliteDB@model@@@@QEAA@XZ) referenced in
function main
- My Questions :
- what is wrong in my design?
- Is there any better way to design the database workflow?
- Edit:
After I Read the links that are provided by Miles Budnek I understood how templates actually works and I know that I have three options to write template class implementation :
Write implementation directly in header file.
write implementation in .cpp file an write explicit instantiations in cpp file.
- Write implementation in .tpp file and include it at the end of header file.
So, I used the second method and wrote explicit instantiation for using model::SqliteDB class: at the end of DataBaseService.cpp
template class DatabaseService<model::SqliteDB>;
This solved the unresolved externals error!
But now I have another error when trying to create an IDatabase pointer for SQliteDB object in DataBaseService.cpp in the line:
m_database = std::make_unique<T>();
Errors :
error: no viable overloaded '='
error: C2679: binary '=': no operator found which takes a right-hand
operand of type 'std::unique_ptr>' (or
there is no acceptable conversion) with [
T=model::SqliteDB,
_Ty=model::SqliteDB ]
c++ polymorphism template-classes
|
show 1 more comment
Sorry if the title is not clear.But, I'll try to explain the problem clearly.
I am working on a c++ and QT using MVC for the application architecture:
- Some description for application structure:
Model Section (designed using polymorphism pattern):
is responsible for database related stuff, It has an IDataBase.h interface and its implementation for sqlite database type.
IDataBase.h :
namespace model{
class IDataBase;
}
class IDataBase
{
public:
virtual ~IDataBase(){
//;
}
virtual bool connect(const QString &db) = 0;
virtual bool insertData(QMap<QString,QVariant> &queryData, const
QString &table) = 0;
};
sqlitedb.h
class SqliteDB: public IDataBase
{
public:
SqliteDB();
~SqliteDB();
bool connect(const QString &db)
bool insertData(QMap<QString,QVariant> &queryData, const QString &table);
private:
QSqlDatabase m_database;
};
Controller Section: I am using the template class DataBaseService to manage any type of databases (mysql, sqlite ....).
DataBaseService.h
// template variable is the database type class
template<class T>
class DatabaseService
{
public:
DatabaseService();
~DatabaseService();
bool addProduct(QMap<QString,QVariant> &queryData, const QString &table);
private:
std::unique_ptr<IDataBase> m_database;
};
DataBaseService.cpp
template<class T>
DatabaseService<T>::DatabaseService()
{
m_database = std::make_unique<T>();
m_database->connect("db.sqlite");
}
- The Problem is :
When I try to create a pointer of class DatabaseService with the template variable SqliteDB in main.cpp file :
DatabaseService<model::SqliteDB> *sqliteDb = new DatabaseService<model::SqliteDB>();
I get This unresolved externals error :
error: LNK2019: unresolved external symbol "public: __cdecl
DatabaseService::DatabaseService(void)"
(??0?$DatabaseService@VSqliteDB@model@@@@QEAA@XZ) referenced in
function main
- My Questions :
- what is wrong in my design?
- Is there any better way to design the database workflow?
- Edit:
After I Read the links that are provided by Miles Budnek I understood how templates actually works and I know that I have three options to write template class implementation :
Write implementation directly in header file.
write implementation in .cpp file an write explicit instantiations in cpp file.
- Write implementation in .tpp file and include it at the end of header file.
So, I used the second method and wrote explicit instantiation for using model::SqliteDB class: at the end of DataBaseService.cpp
template class DatabaseService<model::SqliteDB>;
This solved the unresolved externals error!
But now I have another error when trying to create an IDatabase pointer for SQliteDB object in DataBaseService.cpp in the line:
m_database = std::make_unique<T>();
Errors :
error: no viable overloaded '='
error: C2679: binary '=': no operator found which takes a right-hand
operand of type 'std::unique_ptr>' (or
there is no acceptable conversion) with [
T=model::SqliteDB,
_Ty=model::SqliteDB ]
c++ polymorphism template-classes
2
Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it?
– Miles Budnek
Nov 21 '18 at 1:08
Thanks, i will read this and try to find the problem!.
– Sameer N Ahmad
Nov 21 '18 at 1:14
1
Another related question, maybe a better dup: Why can templates only be implemented in the header file?
– Miles Budnek
Nov 21 '18 at 1:14
I will read this also,however if you have time just give me your opinion about the design of model and controller sections, please!
– Sameer N Ahmad
Nov 21 '18 at 1:19
1
It seems like a reasonable design. If you want a full review I would recommend you post a question over on Code Review once you have everything working. You may want to look into an off-the-shelf ORM or data access library though, if your usage is at all complicated. They'll likely have spent a lot more time and effort than you can to make sure things are free of bugs and security vulnerabilities.
– Miles Budnek
Nov 21 '18 at 2:15
|
show 1 more comment
Sorry if the title is not clear.But, I'll try to explain the problem clearly.
I am working on a c++ and QT using MVC for the application architecture:
- Some description for application structure:
Model Section (designed using polymorphism pattern):
is responsible for database related stuff, It has an IDataBase.h interface and its implementation for sqlite database type.
IDataBase.h :
namespace model{
class IDataBase;
}
class IDataBase
{
public:
virtual ~IDataBase(){
//;
}
virtual bool connect(const QString &db) = 0;
virtual bool insertData(QMap<QString,QVariant> &queryData, const
QString &table) = 0;
};
sqlitedb.h
class SqliteDB: public IDataBase
{
public:
SqliteDB();
~SqliteDB();
bool connect(const QString &db)
bool insertData(QMap<QString,QVariant> &queryData, const QString &table);
private:
QSqlDatabase m_database;
};
Controller Section: I am using the template class DataBaseService to manage any type of databases (mysql, sqlite ....).
DataBaseService.h
// template variable is the database type class
template<class T>
class DatabaseService
{
public:
DatabaseService();
~DatabaseService();
bool addProduct(QMap<QString,QVariant> &queryData, const QString &table);
private:
std::unique_ptr<IDataBase> m_database;
};
DataBaseService.cpp
template<class T>
DatabaseService<T>::DatabaseService()
{
m_database = std::make_unique<T>();
m_database->connect("db.sqlite");
}
- The Problem is :
When I try to create a pointer of class DatabaseService with the template variable SqliteDB in main.cpp file :
DatabaseService<model::SqliteDB> *sqliteDb = new DatabaseService<model::SqliteDB>();
I get This unresolved externals error :
error: LNK2019: unresolved external symbol "public: __cdecl
DatabaseService::DatabaseService(void)"
(??0?$DatabaseService@VSqliteDB@model@@@@QEAA@XZ) referenced in
function main
- My Questions :
- what is wrong in my design?
- Is there any better way to design the database workflow?
- Edit:
After I Read the links that are provided by Miles Budnek I understood how templates actually works and I know that I have three options to write template class implementation :
Write implementation directly in header file.
write implementation in .cpp file an write explicit instantiations in cpp file.
- Write implementation in .tpp file and include it at the end of header file.
So, I used the second method and wrote explicit instantiation for using model::SqliteDB class: at the end of DataBaseService.cpp
template class DatabaseService<model::SqliteDB>;
This solved the unresolved externals error!
But now I have another error when trying to create an IDatabase pointer for SQliteDB object in DataBaseService.cpp in the line:
m_database = std::make_unique<T>();
Errors :
error: no viable overloaded '='
error: C2679: binary '=': no operator found which takes a right-hand
operand of type 'std::unique_ptr>' (or
there is no acceptable conversion) with [
T=model::SqliteDB,
_Ty=model::SqliteDB ]
c++ polymorphism template-classes
Sorry if the title is not clear.But, I'll try to explain the problem clearly.
I am working on a c++ and QT using MVC for the application architecture:
- Some description for application structure:
Model Section (designed using polymorphism pattern):
is responsible for database related stuff, It has an IDataBase.h interface and its implementation for sqlite database type.
IDataBase.h :
namespace model{
class IDataBase;
}
class IDataBase
{
public:
virtual ~IDataBase(){
//;
}
virtual bool connect(const QString &db) = 0;
virtual bool insertData(QMap<QString,QVariant> &queryData, const
QString &table) = 0;
};
sqlitedb.h
class SqliteDB: public IDataBase
{
public:
SqliteDB();
~SqliteDB();
bool connect(const QString &db)
bool insertData(QMap<QString,QVariant> &queryData, const QString &table);
private:
QSqlDatabase m_database;
};
Controller Section: I am using the template class DataBaseService to manage any type of databases (mysql, sqlite ....).
DataBaseService.h
// template variable is the database type class
template<class T>
class DatabaseService
{
public:
DatabaseService();
~DatabaseService();
bool addProduct(QMap<QString,QVariant> &queryData, const QString &table);
private:
std::unique_ptr<IDataBase> m_database;
};
DataBaseService.cpp
template<class T>
DatabaseService<T>::DatabaseService()
{
m_database = std::make_unique<T>();
m_database->connect("db.sqlite");
}
- The Problem is :
When I try to create a pointer of class DatabaseService with the template variable SqliteDB in main.cpp file :
DatabaseService<model::SqliteDB> *sqliteDb = new DatabaseService<model::SqliteDB>();
I get This unresolved externals error :
error: LNK2019: unresolved external symbol "public: __cdecl
DatabaseService::DatabaseService(void)"
(??0?$DatabaseService@VSqliteDB@model@@@@QEAA@XZ) referenced in
function main
- My Questions :
- what is wrong in my design?
- Is there any better way to design the database workflow?
- Edit:
After I Read the links that are provided by Miles Budnek I understood how templates actually works and I know that I have three options to write template class implementation :
Write implementation directly in header file.
write implementation in .cpp file an write explicit instantiations in cpp file.
- Write implementation in .tpp file and include it at the end of header file.
So, I used the second method and wrote explicit instantiation for using model::SqliteDB class: at the end of DataBaseService.cpp
template class DatabaseService<model::SqliteDB>;
This solved the unresolved externals error!
But now I have another error when trying to create an IDatabase pointer for SQliteDB object in DataBaseService.cpp in the line:
m_database = std::make_unique<T>();
Errors :
error: no viable overloaded '='
error: C2679: binary '=': no operator found which takes a right-hand
operand of type 'std::unique_ptr>' (or
there is no acceptable conversion) with [
T=model::SqliteDB,
_Ty=model::SqliteDB ]
c++ polymorphism template-classes
c++ polymorphism template-classes
edited Nov 22 '18 at 11:16
Sameer N Ahmad
asked Nov 21 '18 at 0:54
Sameer N AhmadSameer N Ahmad
206
206
2
Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it?
– Miles Budnek
Nov 21 '18 at 1:08
Thanks, i will read this and try to find the problem!.
– Sameer N Ahmad
Nov 21 '18 at 1:14
1
Another related question, maybe a better dup: Why can templates only be implemented in the header file?
– Miles Budnek
Nov 21 '18 at 1:14
I will read this also,however if you have time just give me your opinion about the design of model and controller sections, please!
– Sameer N Ahmad
Nov 21 '18 at 1:19
1
It seems like a reasonable design. If you want a full review I would recommend you post a question over on Code Review once you have everything working. You may want to look into an off-the-shelf ORM or data access library though, if your usage is at all complicated. They'll likely have spent a lot more time and effort than you can to make sure things are free of bugs and security vulnerabilities.
– Miles Budnek
Nov 21 '18 at 2:15
|
show 1 more comment
2
Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it?
– Miles Budnek
Nov 21 '18 at 1:08
Thanks, i will read this and try to find the problem!.
– Sameer N Ahmad
Nov 21 '18 at 1:14
1
Another related question, maybe a better dup: Why can templates only be implemented in the header file?
– Miles Budnek
Nov 21 '18 at 1:14
I will read this also,however if you have time just give me your opinion about the design of model and controller sections, please!
– Sameer N Ahmad
Nov 21 '18 at 1:19
1
It seems like a reasonable design. If you want a full review I would recommend you post a question over on Code Review once you have everything working. You may want to look into an off-the-shelf ORM or data access library though, if your usage is at all complicated. They'll likely have spent a lot more time and effort than you can to make sure things are free of bugs and security vulnerabilities.
– Miles Budnek
Nov 21 '18 at 2:15
2
2
Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it?
– Miles Budnek
Nov 21 '18 at 1:08
Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it?
– Miles Budnek
Nov 21 '18 at 1:08
Thanks, i will read this and try to find the problem!.
– Sameer N Ahmad
Nov 21 '18 at 1:14
Thanks, i will read this and try to find the problem!.
– Sameer N Ahmad
Nov 21 '18 at 1:14
1
1
Another related question, maybe a better dup: Why can templates only be implemented in the header file?
– Miles Budnek
Nov 21 '18 at 1:14
Another related question, maybe a better dup: Why can templates only be implemented in the header file?
– Miles Budnek
Nov 21 '18 at 1:14
I will read this also,however if you have time just give me your opinion about the design of model and controller sections, please!
– Sameer N Ahmad
Nov 21 '18 at 1:19
I will read this also,however if you have time just give me your opinion about the design of model and controller sections, please!
– Sameer N Ahmad
Nov 21 '18 at 1:19
1
1
It seems like a reasonable design. If you want a full review I would recommend you post a question over on Code Review once you have everything working. You may want to look into an off-the-shelf ORM or data access library though, if your usage is at all complicated. They'll likely have spent a lot more time and effort than you can to make sure things are free of bugs and security vulnerabilities.
– Miles Budnek
Nov 21 '18 at 2:15
It seems like a reasonable design. If you want a full review I would recommend you post a question over on Code Review once you have everything working. You may want to look into an off-the-shelf ORM or data access library though, if your usage is at all complicated. They'll likely have spent a lot more time and effort than you can to make sure things are free of bugs and security vulnerabilities.
– Miles Budnek
Nov 21 '18 at 2:15
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403822%2fbase-class-pointer-pointing-to-a-derived-class-object-which-is-a-template-variab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403822%2fbase-class-pointer-pointing-to-a-derived-class-object-which-is-a-template-variab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it?
– Miles Budnek
Nov 21 '18 at 1:08
Thanks, i will read this and try to find the problem!.
– Sameer N Ahmad
Nov 21 '18 at 1:14
1
Another related question, maybe a better dup: Why can templates only be implemented in the header file?
– Miles Budnek
Nov 21 '18 at 1:14
I will read this also,however if you have time just give me your opinion about the design of model and controller sections, please!
– Sameer N Ahmad
Nov 21 '18 at 1:19
1
It seems like a reasonable design. If you want a full review I would recommend you post a question over on Code Review once you have everything working. You may want to look into an off-the-shelf ORM or data access library though, if your usage is at all complicated. They'll likely have spent a lot more time and effort than you can to make sure things are free of bugs and security vulnerabilities.
– Miles Budnek
Nov 21 '18 at 2:15