1

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...

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Simo Pelle
  • 141
  • 1
  • 11
  • You seemingly want to do full specializations of your template functions. For that you need to add a `template<>` before the definitions. – n314159 Feb 05 '20 at 16:20
  • @n314159 even if I don't use it? for every definitions or just for one? – Simo Pelle Feb 05 '20 at 16:22
  • What does "*An "inline" definition isn't the right way for me because I need that only these types can be called...*" mean exactly? You can use SFINAE/`std::enable_if` to make a template selectively usable for certain types. – walnut Feb 05 '20 at 16:23
  • @stacking Yes, you need to tell the compiler that you want to specialize this function and not override it in another way. – n314159 Feb 05 '20 at 16:24
  • @n314159 thanks, it worked! – Simo Pelle Feb 05 '20 at 16:30

0 Answers0