Thread: Error LNK2019 and Fatal error LNK1120: 1 unresolved externals

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    14

    Error LNK2019 and Fatal error LNK1120: 1 unresolved externals

    Hello,

    I've been trying to find a solution to this problem, but none of them have worked so far. I know which line of code is causing the error, but not how to stop it.

    I google searched for a solution, and tryed a bunch of things, such as making sure my program was a 32console application, and I was calling the header file correctly, as well as trying to find what it was that wasn't linking, but I don't have a lot of experiance, and whatever I'm missing, I can't seem to find it.


    Error Message:

    1>------ Build started: Project: annoting, Configuration: Debug Win32 ------
    1>Main.obj : error LNK2019: unresolved external symbol "int __cdecl Equation(char)" (?Equation@@YAHD@Z) referenced in function _main
    1>C:\Users\User\Documents\Visual Studio 2010\Projects\annoting\Debug\annoting.exe : fatal error LNK1120: 1 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    main.cpp

    Code:
     
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <time.h>
    #include "functions.h"
    using namespace std;
    typedef struct node{
     int num1, num2;
     int data;
     node *next;
    };
    
    int Equation(char oper);
    int main(){
     int enemies=30, age, result, guess, score=0;
     string username;
     char mode;
     node *head=NULL;
     node *temp;
     temp=(node*)malloc(sizeof(node));
     cout<<"Please enter your first name: \t";
     cin>>username;
     cout<<"Please enter your age: \t";
     cin>>age;
     mode =1;
     int i=enemies;
     while(i>0){
      temp->data=Equation(mode);//This is the line I have to take out inorder for the code to compile.
      temp->next=head;
      head=temp;
      i--;
     }
     
     cin>>guess;
     temp=temp->next;
     result=temp->data;
     if(guess==result){
      enemies=enemies-1;
      score=score+1;
      cout<<"\nThere are "<<enemies<<" enemies left!\n\tYour score is "<<score<<"!\n";
     }
     else{
      cout<<"\nThe correct answer was "<<result<<"\n";
     }
     _getch();
     return 0;
    }
    Functions.cpp
    Code:
     
    #include <iostream>
    #include <string>
    #include <time.h>
    #include <conio.h>
    #include "functions.h"
    using namespace std;
     
    int demon::Equation(char oper){
     int int_1, int_2, result;
       srand(time(NULL));
       int_1=1+rand()%5;
       int_2=1+rand()%5;
       if(oper='1'){
        result = int_1+int_2;
        cout<<int_1<<"+"<<int_2<<"=\t";
       }
       else{
        result = int_1-int_2;
        cout<<int_1<<"-"<<int_2<<"=\t";
       }
       return result;
    }
    functions.h
    Code:
    #ifndef Guy_H_
    #define Guy_H_
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <time.h>
    using namespace std;
    
    class Guy{
     protected:
      bool live;
     public:
      int distance;
    };
    class demon: public Guy{
     private:
      int answer;
      char oper;
     public:
      int Equation(char oper);
    };
    #endif
    I am running Microsoft Visual Studios 2010 Express

    Any ideas of what might be causing the problem would help. Thank you.
    Last edited by aileentas; 10-27-2012 at 03:09 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You've only implemented
    int demon::Equation(char oper)

    Not
    ::Equation(char oper)

    What are your demon and Guy classes for?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Oh, okay. I'm not sure what the difference between int demon::Equation(char oper) and ::Equation(char oper) are though. I guess I'll try it out and see.

    I'm supposed to use inheritance classes for the project I'm working on, so what I'm trying to do is have Guy class to be the parent class to two other classes, demon and angel. and then the angel kills the demon class off. Though, I haven't gotten very far yet since I've been trying to fix this error.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Okay, I tryed adding ::Equation(char oper) to my function but I got the following error:


    error C2039: 'Equation' : is not a member of '`global namespace''

    I thought that since I'm using namespace std, I should put std::Equation(char oper)

    But then I got:

    error C2039: 'Equation' : is not a member of 'std'

    Is there some other syntax I'm supposed to use?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Delete your classes and just implement
    Code:
    int Equation(char oper){
      // your code
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Okay. That worked.

    However, what is the method to using funtions in inheritance classes? I don't get any errors when I use them in regular classes, but for some reason when I use them in an iheritance class I get a whole bunch of errors and I can't seem to figure out how to fix them properly.

    The most common thing I saw when searching for a solution looks pretty much the same as

    Code:
    ElementType ClassName::FunctionName(){
    }
    But, if I try to call it in my main function it can never find the function.
    Last edited by aileentas; 10-27-2012 at 05:43 AM.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    14
    Nevermind my last post. I figuered out what I was doing wrong. Thank you so much for the help! =)

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you supposed to use some linked list? If so, there is std::list.
    Also, you have a habit of placing all your variables at the beginning of the function. Don't do that. Instead, declare them near first use. This is C++, after all.
    Don't use malloc in C++ unless you have a good reason. However, at this stage of your career, you don't, so don't do it.
    Also, all new must be matched with a delete (like malloc must be matched with free). Or you could just use a smart pointer. Google it. [Hint: std::shared_ptr, std::unique_ptr, std::weak_ptr]
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error LNK1120: 1 unresolved externals
    By ncode in forum C Programming
    Replies: 2
    Last Post: 08-01-2011, 11:21 PM
  2. fatal error LNK1120: 2 unresolved externals
    By SgtPooki in forum C++ Programming
    Replies: 10
    Last Post: 08-09-2010, 07:28 AM
  3. error LNK2019: unresolved external symbol
    By kiros88 in forum C++ Programming
    Replies: 7
    Last Post: 06-16-2010, 01:16 PM
  4. Fatal Error LNK1220: 1 unresolved externals
    By zenghoong in forum C Programming
    Replies: 3
    Last Post: 10-12-2009, 03:24 AM
  5. error LNK2019: unresolved external symbol
    By Opel_Corsa in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2006, 12:12 PM