Thanks.
I ordered the Lambda book, gonna need it.
Incidentally, I just read about structured bindings in Professional C++ p38 covers it.
QUESTION 13)
Is it possible to add a template to a singleton? I have not done templates on header files before, but I was able to get this far. It compiles with errors "3 unresolved externals"
I know the compiler has to resolve the templates, but on singletons..tricky business???
With this line, looks like I am asking to use <T> without it even first having made an instance of the class...no good.
|
static President<T> onlyInstance;
| |
But I really don't want to use the line below as it defeats the purpose & it does not work anyway.
|
static President<vector<string>> onlyInstance;
| |
"unresolved external symbol "public: static class President<class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > & __cdecl President<class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > >::GetInstance(void)" (?GetInstance@?$President@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@@@SAAAV1@XZ) referenced in function _main"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
#include <vector>
#include "President.h"
using namespace std;
int main()
{
President<vector<string>>& onlyPresident =
President<vector<string>>::GetInstance();
//President& onlyPresident = onlyPresident.GetInstance();
onlyPresident.SetName("Abe Lincoln");
cout << President<vector<string>>::GetInstance().GetName() << endl;
cout << onlyPresident.GetName() << endl;
cout << onlyPresident.GetInstance().GetName() << endl;
}
| |
President.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#pragma once
///#pragma warning (disable : XXXXX)
#include <string>
using namespace std;
template<typename T>
class President
{
President() {}; //private default constructor
President(const President&); //private copy constructor
const President& operator = (const President&) = delete; // assignment operator
string name;
T containerFormerPresidents;
public:
static President<T>& GetInstance();
const string GetName() const;
void SetName(string inputName);
};
| |
President.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include "President.h"
template<typename T>
President<T>& President<T>::GetInstance()
{
//static ensures only happens once
static President<T> onlyInstance;
//static President<vector<string>> onlyInstance;
return onlyInstance;
}
template<typename T>
const string President<T>::GetName() const
{
return name;
}
template<typename T>
void President<T>::SetName(string inputName)
{
name = inputName;
}
| |