nafiseh
nafiseh

Reputation: 129

help with error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

i have recently come to this error , i search a lot but i could not find the solution , i made a win 32 console application(c++) in visual studio 2010 and added 2 header files and 1 .cpp file with main function in it , i did not change the project's properties , your help would be really appreciated. here is my code:

//listNode.h:
#ifndef LISTNODE_H

#define LISTNODE_H

template< typename item > class Node
{

private:

    item data;
    Node<item> *next;

public:

    Node( item d , Node<item> *n );
    void setData( item d );
    item getData();
    void setNext( Node *n );
    Node<item>* getNext();

};

template< typename item > 
    Node< item > :: Node( item d , Node *n )
    {
        data = d;
        next = n;
    }

    template< typename item>
    void Node< item > :: setData( item d )
    {
        data = d;
    }

    template< typename item>
    item Node< item > :: getData()
    {
        return data;
    }

    template< typename item >
    void Node< item > :: setNext( Node *n )
    {
        next = n;
    }

    template< typename item >
    Node<item>* Node<item> :: getNext()
    {
        return next;
    }

#endif  


//list:
#ifndef LIST_H
#define LIST_H

#include "listNode.h"

template < typename item > class List
{
private:

    Node<item> *first;
    Node<item> *last;
    int numberOfElements;

public:

    List( Node<item> *f , Node<item> *l , int num );
    ~List();
    void addFront( item d );
    void addRear( item d );
    void removeFront();
    void removeRear();
    item front();
    item rear();
    bool isEmpty();
    int size();
    void print();

};


template < typename item >
List<item> :: List( Node<item> *f , Node<item> *l , int num )
{
    first = f;
    last = l;
    numberOfElements = num;
}

template < typename item >
List<item> :: ~List()
{
    for( int i=0 ; i<nummerOfElements ; i++ )
    {
        Node<item> temp = *first;

        delete *first;

        first = temp.getNext();

    }

    first = 0;
    last = 0;
    numberOfElements = 0;
}



template < typename item >
void List<item> :: addFront( item d )
{
    Node<item> newNode( d , first );

    first = &newNode;

    numberOfElements++;
}

template < typename item >
void List<item> :: addRear( item d )
{
    Node<item> newNode;
    newNode.setData(d);

    if( numberOfElements != 0 )
        *last.setNext( &newNode );

    last = &newNode;

    numberOfElements++;
}


template < typename item >
void List<item> :: removeFront()
{
    if( numberOfElements != )
    {

        Node<item> temp = *first;

        delete *first;

        first = temp.getNext();

        delete temp;

        numberOfElements--;
    }

    else
        cout << "list is already empty!\n"
}

template < typename item >
void List<item> :: removeRear()
{
    if( numnerOfElements != 0 )
    {

        //finding the node before last : 

        Node<item> *beforeLast;

        beforeLast = first;

        while( true ) 
      {
          if( *beforeLast.getNext() == last )
              break;

          beforeLast = *beforeLast.getNext();
      }


        Node<item> temp = *last;

        delete *last;

        last = beforeLast;

        delete temp;

        numberOfElements--;
    }

    else
        cout<< "list is already empty!\n"
}

template < typename item >
item List<item> :: front()
{
    if( numberOfElements != 0 )
        return *first.getData();
    else
        return
        -1;
}

template < typename item >
item List<item> :: rear()
{
    if( numberOfElements != 0 )
        return *last.getData();
    else
        return -1;
}

template < typename item >
bool List<item> :: isEmpty()
{
    if( numberOfElements == 0 )
        true;
    else
        false;
}

template < typename item >
int List<item> :: size()
{
    return numberOfElements;
}



#endif


//main.cpp:

#include <string>
#include<iostream>

using namespace std;

#include "list.h"

template < typename item >
int main()
{
    string atom1("1");
    string atom2("2");
    string atom3("3");
    string atom4("5");
    string atom5("6");

    List<item> l1();

    l1.addRear( atom4 );
    l1.addRear( atom5 );

    List<item> l2();

    l2.addRear( atom1 );
    l2.addRear( atom2 );
    l2.addRear( atom3 );
    l2.addRear( l1 );

    cout << l2.front()


        return 0;
}

Upvotes: 0

Views: 4818

Answers (2)

Greg Domjan
Greg Domjan

Reputation: 14105

error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

A Windows App has ___tmainCRTStartup as one of the CRT functions it is running in startup which calls into your main function WinMain sometimes expresses as TWinMain to support dual compilation ascii/widechar.

In comparison a console app uses main

You are missing WinMain So you have either the wrong type of project, or the wrong main method.
Then there is the problem already described that you cannot use template on main

Upvotes: 0

user2100815
user2100815

Reputation:

main is a special function in C++ - it cannot be a template. And if it could be, how would you provide the type(s) it is templated on? I think maybe you don't quite understand templates yet.

I have just checked this by creating a VC++ console project with a main that looks like this:

template <typename T>
int main() {}

and I get exactly the link error message you get, but no compilation error (which there should be).

Upvotes: 2

Related Questions