提问者:小点点

需要帮助重新处理错误LNK2019和LNK1120[副本]


得到这两个我似乎无法修复的错误。有什么想法吗?

1>-------生成已开始:project:final,Configuration:Debug Win32------1>msvcrtd.lib(exe_main.obj):错误LNK2019:函数“int__cdecl invoke_main(void)”(?invoke_main@@yahxz)1>C:\users\name\source\repos\final\Debug\final.exe引用的未解析外部符号_main:致命错误LNK1120:1未解析外部1>完成生成项目“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";
    }
 
}


 
 

共1个答案

匿名用户

入口点main()应声明为not为template。

模板应从

template<typename T>
int main() {