提问人:Simo Pelle 提问时间:2/6/2020 最后编辑:1201ProgramAlarmSimo Pelle 更新时间:2/6/2020 访问量:296
模板函数中未解析的外部符号
Unresolved external symbol in template function
问:
我在 Visual Studio 2019 中创建了这个静态库,但出现以下错误:
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) });
}
我在 Lib.h 中还收到两个警告:
未找到“方法 1”的函数定义。 未找到“方法 2”的函数定义。
我做错了什么?我已经遵循了这些说明(替代解决方案)。
“内联”定义对我来说不是正确的方式,因为我需要只有这些类型才能被调用......
答: 暂无答案
评论
template<>
std::enable_if