Thread: Error: unresolved external symbol

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question Error: unresolved external symbol

    Hey there,
    I am building my own class called "greetings"..
    When executing the application program, I get the following three errors..
    What should I do? and what do those errors mean?


    hellotest.obj : error LNK2001: unresolved external symbol "public: __thiscall greetings::~greetings(void)" (??1greetings@@QAE@XZ)
    hellotest.obj : error LNK2001: unresolved external symbol "public: __thiscall greetings::greetings(char const *)" (
    ??0greetings@@QAE@PBD@Z)
    Debug/hellotest.exe : fatal error LNK1120: 2 unresolved externals
    It ain't illegal until you get caught..

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Looks like you may have declared but not defined those two class constructor/destructor member functions. We would need to see your code (the class related code) to make sure.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Code:
     // File: hello.h Greetings class specification 
    #ifndef HELLO_H
    #define HELLO_H
    class greetings{
    public:
    greetings(const char*); // constructor
    ~greetings(void); // destructor
    private:
    char *ModuleString; // string to print
    };
    #endif
    Code:
     // File: hello.cpp Source code for greetings objects 
    #include <iostream.h>
    #include <string.h>
    #include "hello.h"
    // Explicit constructor prints greetings and string argument, which it stores in data member
    // Pre: str is a string.
    // Post: The object greetings has been constructed and a welcome message printed
    greetings::greetings(const char *str)
    {
    ModuleString = new char[strlen(str) + 1];
    strcpy(ModuleString, str);
    cout<<"Welcome to "<<str<<"!\n";
    }
    // Destructor prints exit greetings and string argument and deletes area to which data member points
    // Pre: none.
    // Post: A good-bye message has been printed and the object has been destroyed.
    greetins::~greetings(void)
    {
    cout<<"Good-bye from "<<ModuleString<<".\n";
    delete [] ModuleString;
    }
    Code:
     // Program to test greetings class 
    #include <iostream.h>
    #include "hello.h"
     
    void subprog(void);
     
    int main(void)
    {
    greetings hi("main");
    cout<<"	main: statements before the call to subprog\n";
    subprog();
    cout<<"	main: statements after the call to subprog\n";
     
    return 0;
    }
     
    // Function to create an instance of the greetings class
    // Pre: none
    // Post: A greetings object has been created and destroyed
    void subprog(void)
    {
    greetings s("subprog");
    cout<<" subprog: statements in subprog\n";
    }
    It ain't illegal until you get caught..

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Ok... that's a new piece of information. Splitting your class' implementation from the test code could be causing this. Are you sure you are compiling and linking the hello.cpp source file with the hellotest.cpp file?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    I have included "hello.cpp" in the application program, and everything is working now.. According to your new information.
    But I have been only including the header file of the class ("hello.h" in this case), in the application program, and everything was working fine with other classes and other programs.. Why did I have to include the implementation of the class in the application program in this case?
    Last edited by amirahasanen1; 10-07-2005 at 09:00 AM.
    It ain't illegal until you get caught..

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You must include every source file so that they can be compiled. Each source file in a program is compiled separately and then lnked together at the end. Adding your header file to the project doesn't really do anything except tell you that it is part of the project, but adding the source file is important if it has any definitions that you use.

    BTW, if you meant that you fixed the problem with #include "hello.cpp" in your test file, then that was the wrong solution. Your code is fine except that you just need to add hello.cpp to your project or makefile so that it will compile as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM