0

1>------ Build started: Project: Seminarski, Configuration: Debug Win32 ------

1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup

1>C:\Users-- : fatal error LNK1120: 1 unresolved externals

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

These are the errors, I tried many of solutions offered here, most being changing linker subsystem and application type, which were both set correctly in the begining.

#include <iostream>
#include <string>

using namespace std;

template<class T>
class Skup
{
private:
    int n, kap;
    T *p;
public:
    explicit Skup(int N) : kap(N), n(0), p(new T[kap]){}
    Skup(const Skup &x);
    ~Skup(){ delete[] p; }
    Skup &operator = (const Skup &x);
    bool provjera(const T &clan);
    void SetClan(int clan);
    T Getn();
    void sortiranje();
    friend ostream& operator << (ostream& izlaz, Skup x);
};

template<class T>
Skup<T>::Skup(const Skup &x)
{
    n = x.n;
    p = new T[n];
    for (int i = 0; i < n; i++)
        p[i] = x.p[i];
}

template<class T>
Skup<T> &Skup<T>::operator = (const Skup &x)
{
    if (this != &x){
        delete[] p;
        n = x.n;
        p = new T[n];
        for (int i = 0; i < n; i++)
            p[i] = x.p[i];
    }
    return *this;
}

template<class T>
bool Skup<T>::provjera(const T &clan)
{
    
    for (int i = 0; i < n; i++)
    {
        if (p[i] == clan)
            return true;

    }
    return false;
}

template<class T>
void Skup<T>::SetClan(int clan)
{
    if (n == kap)
        throw "Kapacitet popunjen!";

    if (!provjera(clan))
        p[n++] = clan;
}

template<class T>
T Skup<T>::Getn()
{
    return n;
}

template<class T>
void Skup<T>::sortiranje()
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - 1; j++)
        {
            if (p[i]<p[j])
            {
                T tmp = p[i];
                p[i] = p[j];
                p[j] = tmp;
            }
        }
    }
}

template<class T>
ostream& operator << (ostream& izlaz, Skup<T> x)
{   
    izlaz << "{"; 
    for (int i = 0; i < x.n-1; i++)
    {
        izlaz << x.p[i];
        izlaz << ", ";
    } 
    izlaz << x.p[x.n-1] << "}" << endl;
    return izlaz;
    };

template < class T > 
int main()
{
    
    int kapa;
    cout << "Kapacitet:";
    cin >> kapa;
    Skup<T> jedan(kapa);
    
    try{
        int BrEl; // za iznimku, unijeti BrEl > kapa
        cout << "Broj elemenata:";
        cin >> BrEl;
        for (int i = 0; i < BrEl; i++)
        {
            int *cl = new int;
            cout << "Dodaj clana:";
            cin >> *cl;
            if (jedan.provjera(*cl))
            {
                i--;
                cout << "Element vec postoji!" << endl;
            }
            else
                jedan.SetClan(*cl);
            delete cl;
        }
    }
    catch(const char* iznimka){
        cout << endl << iznimka << endl;
    }
    
    jedan.sortiranje();
    cout << jedan;

    system("pause");
    return 0;
}

Sorry for comments on Croatian, I'm sure you can figure out the solution without that :)

Community
  • 1
  • 1
ante_f
  • 200
  • 11
  • 3
    What is it you were trying to achieve by making `main` a function template rather than a function? (You cannot do that, which is why you get the error) – Wintermute Jun 05 '15 at 16:45
  • @nytez I would declare main as a friend of class Skup.:) At least the code could be compiled. – Vlad from Moscow Jun 05 '15 at 16:49
  • @Wintermute can you tell me how to fix it? – ante_f Jun 05 '15 at 16:52
  • 2
    Knowing Croatian would help me here, but I believe mainly you have to remove the `template` from `main`'s declaration (so that it is no longer a template but a function) and use `Skup` instead of `Skup` inside. It looks like you want a `Skup`. I think. – Wintermute Jun 05 '15 at 16:57
  • Thanks a lot dude! @Wintermute :) – ante_f Jun 05 '15 at 17:01

1 Answers1

1

Don't declare main as a function template.

Instead, give a proper type to Skud.

KABoissonneault
  • 2,359
  • 18
  • 17
  • can you please tell me how to change it, so that im still able to use Skup in main? @kaboissonneault – ante_f Jun 05 '15 at 16:55
  • Function template, not template function. The distinction is quite important here. The way it is declared in the question, `main` is not a function. – Wintermute Jun 05 '15 at 17:00
  • @Wintermute now it compiles when i comment "cout << jedan" in main, but now obviously my operator << doesnt work, can you tell me how to fix that so it works in main? it was working when i was using int instead of templates :/ – ante_f Jun 05 '15 at 17:13