I've created this static library in Visual Studio 2019 but I get the following error:
unresolved external symbol "unsigned short __cdecl method2<unsigned short>(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>> const &,unsigned short)"
??$method2@G@@YAGABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@G@Z)
//Lib.h
#pragma once
#include "Enter.h"
#include <iostream>
#include <string>
template <typename T>
T method1(const std::string& = "", T = -1);
template <typename T>
T method2(const std::string& = "", T = -1);
//Lib.cpp
#include "pch.h"
#include "Lib.h"
using namespace std;
unsigned short method1(const string& text, unsigned short max)
{
string ins;
unsigned short val;
while (true)
{
cout << text;
getline(cin, ins);
try
{
if (cin.fail()) cin.clear();
val = stoi(ins);
if (val > max) throw val;
}
catch (...)
{
cerr << "Risposta non valida!";
Enter(2);
continue;
}
break;
}
return val;
}
unsigned method1(const string& text, unsigned max)
{
string ins;
unsigned val;
while (true)
{
cout << text;
getline(cin, ins);
try
{
if (cin.fail()) cin.clear();
val = stoi(ins);
if (val > max) throw val;
}
catch (...)
{
cerr << "Risposta non valida!";
Enter(2);
continue;
}
break;
}
return val;
}
unsigned long method1(const string& text, unsigned long max)
{
string ins;
unsigned long val;
while (true)
{
cout << text;
getline(cin, ins);
try
{
if (cin.fail()) cin.clear();
val = stoi(ins);
if (val > max) throw val;
}
catch (...)
{
cerr << "Risposta non valida!";
Enter(2);
continue;
}
break;
}
return val;
}
unsigned long long method1(const string& text, unsigned long long max)
{
string ins;
unsigned long long val;
while (true)
{
cout << text;
getline(cin, ins);
try
{
if (cin.fail()) cin.clear();
val = stoi(ins);
if (val > max) throw val;
}
catch (...)
{
cerr << "Risposta non valida!";
Enter(2);
continue;
}
break;
}
return val;
}
unsigned short method2(const string& text, unsigned short max)
{
unsigned short val;
while (true)
{
cout << text;
if (cin >> val && val <= max) break;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Risposta non valida!";
Enter(2);
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return val;
}
unsigned method2(const string& text, unsigned max)
{
unsigned val;
while (true)
{
cout << text;
if (cin >> val && val <= max) break;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Risposta non valida!";
Enter(2);
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return val;
}
unsigned long method2(const string& text, unsigned long max)
{
unsigned long val;
while (true)
{
cout << text;
if (cin >> val && val <= max) break;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Risposta non valida!";
Enter(2);
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return val;
}
unsigned long long method2(const string& text, unsigned long long max)
{
unsigned long long val;
while (true)
{
cout << text;
if (cin >> val && val <= max) break;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Risposta non valida!";
Enter(2);
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return val;
}
//main.cpp
#include <vector>
struct Client
{
std::string name;
unsigned short age;
};
int main()
{
std::vector<Client> mylist;
mylist.push_back({ temp, method2<unsigned short>("Insert age: ", 120) });
}
I also get two warnings in Lib.h:
Function definition for 'method 1' not found. Function definition for 'method 2' not found.
What am I doing wrong? I've followed these instructions (alternative solution).
An "inline" definition isn't the right way for me because I need that only these types can be called...