6 Replies - 468 Views - Last Post: 24 February 2012 - 11:52 AM Rate Topic: ***** 1 Votes

#1 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

LNK2019 and LNK1120 ? ow to Solve?

Posted 23 February 2012 - 10:27 AM

Asak Friends, I'm using Visual C++ 2008 Express Edition and i'm having a linker error in every program that i'm making in it. I'm not able to understand what's the actual problem. I have searched a lot on different blogs or websites. but still i'm unable to understand the linker error LNK2019 and LNK1120 . Here is my code. Help me to solve it!


//stdafx.h header file


#include<iostream>
using namespace std;
template<class T>

int search(T Array[], T key, int size)
{
    for (int i=0; i<size; i++)
    {
        if (Array[i]==key)
        {
            return i;
        }
    return -1;
    }
}



That's the main file.

#include"stdafx.h"
using namespace std;
template<class T>
void main()
{
    char chrArr[] = {'a', 'c', 'f', 's', 'u', 'z'};
    char ch = 'f'; // value to find
    int intArr[] = {1, 3, 5, 9, 11, 13};
    int in = 6;
    double dubArr[] = {1.0, 3.0, 5.0, 9.0, 11.0, 13.0};
    double db = 4.0;
    cout << "\n 'f' in chrArray: index=" << find(chrArr, ch, 6);
    cout << "\n 6 in intArray: index=" << find(intArr, in, 6);
    cout << "\n 4 in dubArray: index=" << find(dubArr, db, 6);
    system("pause");
}



Is This A Good Question/Topic? 0
  • +

Replies To: LNK2019 and LNK1120 ? ow to Solve?

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: LNK2019 and LNK1120 ? ow to Solve?

Posted 23 February 2012 - 10:47 AM

Main should return an int and not void. Also, you should never put a using directive (using namespace std;) in a header file. You need to include your header file to be able to use it in the main file.

EDIT: You named the function in your header file "search" but then you call a function "find" three times in main. Did you mean search?

This post has been edited by vividexstance: 23 February 2012 - 10:48 AM

Was This Post Helpful? 1
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: LNK2019 and LNK1120 ? ow to Solve?

Posted 23 February 2012 - 10:59 AM

Quote

#include"stdafx.h"
using namespace std;
template<class T>
void main()
{
// ...


You can't declare main to be a template. Your compiler should be halting at the third line and never get far enough to produce linker errors.

Get rid of that "template ..." line.
Was This Post Helpful? 1
  • +
  • -

#4 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: LNK2019 and LNK1120 ? ow to Solve?

Posted 23 February 2012 - 11:06 AM

View Postvividexstance, on 23 February 2012 - 10:47 AM, said:

Main should return an int and not void. Also, you should never put a using directive (using namespace std;) in a header file. You need to include your header file to be able to use it in the main file.

EDIT: You named the function in your header file "search" but then you call a function "find" three times in main. Did you mean search?


Ok. After Fixing, what you have said. i'm still having the same problem linker error..

 #include<iostream>
template<class T>

int search(T Array[], T key, int size)
{
    for (int i=0; i<size; i++)
    {
        if (Array[i]==key)
        {
            return i;
        }
    return -1;
    }
}



 #include"stdafx.h"
using namespace std;
template<class T>
int main()
{
    char chrArr[] = {'a', 'c', 'f', 's', 'u', 'z'};
    char ch = 'f'; // value to find
    int intArr[] = {1, 3, 5, 9, 11, 13};
    int in = 6;
    double dubArr[] = {1.0, 3.0, 5.0, 9.0, 11.0, 13.0};
    double db = 4.0;
    cout << "\n 'f' in chrArray: index=" << search(chrArr, ch, 6);
    cout << "\n 6 in intArray: index=" << search(intArr, in, 6);
    cout << "\n 4 in dubArray: index=" << search(dubArr, db, 6);
    system("pause");
}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: LNK2019 and LNK1120 ? ow to Solve?

Posted 23 February 2012 - 11:15 AM

View Postr.stiltskin, on 23 February 2012 - 10:59 AM, said:

Quote

#include"stdafx.h"
using namespace std;
template<class T>
void main()
{
// ...


You can't declare main to be a template. Your compiler should be halting at the third line and never get far enough to produce linker errors.

Get rid of that "template ..." line.



Thank you sir, Its working .. :-)
Was This Post Helpful? 0
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 390
  • View blog
  • Posts: 1,349
  • Joined: 31-December 10

Re: LNK2019 and LNK1120 ? ow to Solve?

Posted 23 February 2012 - 11:47 AM

Here's a little tip, don't include the iostream in your header, you don't use it there, but put it in the main file where you actually use it. The reason for this is because whenever someone includes your header file, they will also be including any header files that are included in your header file. You want to keep your file size down, and not to include things you don't need. Since you don't use iostream in your header file, there is no reason to include it there.
Was This Post Helpful? 1
  • +
  • -

#7 joannesablad  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 24-April 11

Re: LNK2019 and LNK1120 ? ow to Solve?

Posted 24 February 2012 - 11:52 AM

View Postvividexstance, on 23 February 2012 - 11:47 AM, said:

Here's a little tip, don't include the iostream in your header, you don't use it there, but put it in the main file where you actually use it. The reason for this is because whenever someone includes your header file, they will also be including any header files that are included in your header file. You want to keep your file size down, and not to include things you don't need. Since you don't use iostream in your header file, there is no reason to include it there.


Thanks Sir. I won't use it again.. !!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1