-1

getting these two errors which I can not seem to fix. Any ideas?

1>------ Build started: Project: final, Configuration: Debug Win32 ------ 1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) 1>C:\Users\name\source\repos\final\Debug\final.exe : fatal error LNK1120: 1 unresolved externals 1>Done building project "final.vcxproj" -- FAILED.

#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";
    }
 
}


 
 
Dodappy
  • 1
  • 1
  • 1
    `main` can't be a template. Just drop `template` in front of it, it makes no sense there. – Igor Tandetnik Apr 18 '21 at 15:19
  • 1
    There exists `std::swap`, you may get unexecpted results due to frivolous use of `using namespace std;`. Both [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [`#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h?noredirect=1) are considered bad practice, especially when you use both together. – Yksisarvinen Apr 18 '21 at 15:21
  • Copy / pasta error we can suspect?? – πάντα ῥεῖ Apr 18 '21 at 15:24

1 Answers1

2

The entry point main() should be declared not as template.

template<typename T> should be removed from

template<typename T>
int main() {
MikeCAT
  • 73,922
  • 11
  • 45
  • 70