c++ - 需要帮助解决错误 LNK2019 和 LNK1120
问题描述
得到这两个我似乎无法修复的错误。有任何想法吗?
1>----- 构建开始:项目:最终,配置:调试 Win32 ------ 1>MSVCRTD.lib(exe_main.obj):错误 LNK2019:未解析的外部符号 _main 在函数“int __cdecl invoke_main”中引用(void)" (?invoke_main@@YAHXZ) 1>C:\Users\name\source\repos\final\Debug\final.exe : 致命错误 LNK1120: 1 unresolved externals 1>Done building project "final.vcxproj" - - 失败的。
#include <bits/stdc++.h>
using namespace std;
template<typename T>
void swap(T* xp, T* yp)
{
T temp = *xp;
*xp = *yp;
*yp = temp;
}
template<typename T>
int linearSearch(T ar[], int n, T key, int start = 0, int end = 2) {
for (int i = start; i <= end; i++)
if (ar[i] == key)
return i;
return -1;
}
template<typename T>
void bubbleSort(T ar[], int n) {
T temp = 0;
cout << "Array sorted using bubble sort \n";
for (int i = 0; i < n; i++) {
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (ar[j] > ar[j + 1])
swap(&ar[j], &ar[j + 1]);
swapped = true;
}
if (swapped == false) break;
}
}
template<typename T>
void selectionSort(T ar[], int n) {
cout << "Array sorted using selection sort \n";
int min = 0;
for (int i = 0; i < n; i++) {
min = i;
for (int j = i + 1; j < n; j++)
if (ar[j] < ar[min])
min = j;
swap(&ar[min], &ar[i]);
}
}
template<typename T>
int binarySearch(T ar[], int lo, int hi, T x) {
if (hi >= lo) {
int mid = lo + (hi - lo) / 2;
if (ar[mid] == x) return mid;
if (ar[mid] > x) return binarySearch(ar, lo, mid - 1, x);
return binarySearch(ar, mid + 1, hi, x);
}
return -1;
}
template<typename T>
void print(T ar[], int n) {
for (int i = 0; i < n; i++)
cout << ar[i] << " ";
cout << endl;
}
template<typename T>
int main() {
int ch;
cout << "Enter 1 for int \t\t 2 for double \t\t 3 for string" << endl;
cin >> ch;
switch (ch) {
case 1:
{
int n;
cout << "Enter length of the array" << endl;
cin >> n;
int ar[n];
cout << "Enter the elements \n";
for (int i = 0; i < n; i++) cin >> ar[i];
int key;
cout << "Enter Number to be searched \n";
cin >> key;
if (linearSearch(ar, n, key, 0, n - 1) != -1) cout << "linear search :: Element found at index " << linearSearch(ar, n, key, 0, n - 1) << endl;
else cout << "Element not found \n";
bubbleSort(ar, n);
selectionSort(ar, n);
if (binarySearch(ar, 0, n - 1, key) != -1) cout << "binary search :: Element found at index " << binarySearch(ar, 0, n - 1, key) << endl;
else cout << "Element not found \n";
print(ar, n);
}
break;
case 2: {
int n;
cout << "Enter length of the array" << endl;
cin >> n;
double ar[n];
cout << "Enter the elements \n";
for (int i = 0; i < n; i++) cin >> ar[i];
double key;
cout << "Enter Number to be searched \n";
cin >> key;
if (linearSearch(ar, n, key, 0, n - 1) != -1) cout << "linear search :: Element found at index " << linearSearch(ar, n, key, 0, n - 1) << endl;
else cout << "Element not found \n";
bubbleSort(ar, n);
selectionSort(ar, n);
if (binarySearch(ar, 0, n - 1, key) != -1) cout << "binary search :: Element found at index " << binarySearch(ar, 0, n - 1, key) << endl;
else cout << "Element not found \n";
print(ar, n);
}
break;
case 3: {
int n;
cout << "Enter length of the array" << endl;
cin >> n;
string ar[n];
cout << "Enter the elements \n";
for (int i = 0; i < n; i++) cin >> ar[i];
string key;
cout << "Enter word to be searched \n";
cin >> key;
if (linearSearch(ar, n, key, 0, n - 1) != -1) cout << "linear search :: Element found at index " << linearSearch(ar, n, key, 0, n - 1) << endl;
else cout << "Element not found \n";
bubbleSort(ar, n);
selectionSort(ar, n);
if (binarySearch(ar, 0, n - 1, key) != -1) cout << "binary search :: Element found at index " << binarySearch(ar, 0, n - 1, key) << endl;
else cout << "Element not found \n";
print(ar, n);
}
break;
default: cout << "Wrong choice \n";
}
}
解决方案
入口点main()
不应声明为模板。
template<typename T>
应该从
template<typename T>
int main() {
推荐阅读
- node.js - 如何声明可以是布尔值或字符串的 Mongoose 字段?
- javascript - 如何在移动版本中合并两个单独的 HTML 部分
- ionic3 - [[16.5.0,16.5.0], [16.4.0,16.4.0]] 的各种其他库的 ionic3 错误 com.google.android.gms:play-services-measurement-base?
- azure-functions - 如何使用 azure 函数在 azure iot Hub 中获取在线和离线设备状态
- documentum - 文档休息服务问题
- ms-word - Word 插件卡在上下文同步中
- php - 将可折叠列表插入 Woocommerce 表
- javascript - 更改 javascript 方法内容会出现“无法读取未定义的属性‘createDocumentFragment’”错误
- javascript - 如何使用 Vuex 有效管理数组元素?
- azure-devops - 拉取请求 - 确保在允许 PR 完成之前关闭链接的任务