Всем привет. У меня тут такое дело:
// learncpp.cpp : main project file.
#include "stdafx.h"
int main()
{
const int LENGTH = 5;
setlocale(LC_ALL,"Russian");
int a = 1, b = 2;
printf("%d, %d\n",a,b);
swap(a,b);
printf("%d, %d\n",a,b);
char c[LENGTH] = "lol";
char d[LENGTH] = "stas";
printf("%s, %s\n",c,d);
swap(c,d, LENGTH);
printf("%s, %s\n",c,d);
_getch();
return 0;
}
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
// TODO: reference additional headers your program requires here
#include <stdio.h>
#include <conio.h>
#include <clocale>
template <typename any> void swap(any &a, any &b);
template <typename any> void swap(any *a, any *b, int n);
// stdafx.cpp : source file that includes just the standard includes
// learncpp.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
template <typename any> void swap(any &a, any &b)
{
any temp = a;
a = b;
b = temp;
}
template <typename any> void swap(any *a, any *b, int n)
{
any temp;
for (int i=0;i<n;i++)
{
temp=a[i];
a[i]=b[i];
b[i]=temp;
}
}
Компилируем... опана!
1>------ Build started: Project: learncpp, Configuration: Debug Win32 ------
1> stdafx.cpp
1> AssemblyInfo.cpp
1> learncpp.cpp
1> Generating Code...
1> .NETFramework,Version=v4.0.AssemblyAttributes.cpp
1>learncpp.obj : error LNK2028: unresolved token (0A00000B) "void __cdecl swap<char>(char *,char *,int)" (??$swap@D@@$$FYAXPAD0H@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>learncpp.obj : error LNK2028: unresolved token (0A00000D) "void __cdecl swap<int>(int &,int &)" (??$swap@H@@$$FYAXAAH0@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>learncpp.obj : error LNK2019: unresolved external symbol "void __cdecl swap<char>(char *,char *,int)" (??$swap@D@@$$FYAXPAD0H@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>learncpp.obj : error LNK2019: unresolved external symbol "void __cdecl swap<int>(int &,int &)" (??$swap@H@@$$FYAXAAH0@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>D:\progs\vcpp\projects\learncpp\Debug\learncpp.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Если же засунуть всё в один файл, то код делает всё правильно. Почему так? Гуглил, гуглил, не нашёл, в чём проблема. Где-то вроде говорят надо библиотеку подключить какую-то, то ли user32.dll, то ли ещё какую.