unresolved external symbol

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pat270881

    unresolved external symbol

    hello,

    I use the Visual C++ 2005 Express Edition and I want to implement a
    simple c++ programm which has a header file and a implementation file.

    the header file:

    Code:
    #include <iostream>
    
    namespace main_savitch_2C
    {
    class statistician
    {
    public:
    void next(double r);
    
    private:
    int count;
    };
    }
    and the implementation file which contains now only the definitions:
    Code:
    #include "stats.h"
    
    using namespace main_savitch_2C;
    
    
    void statistician::next(double r)
    {
    
    }
    But when I try to compile this very simple program, the following error
    occurs:

    ------ Build started: Project: Test, Configuration: Debug Win32 ------
    Compiling...
    statsimpl.cpp
    Linking...
    msvcrtd.lib(crt exe.obj) : error LNK2019: unresolved external symbol
    _main referenced in function ___tmainCRTStar tup
    C:\Dokumente und Einstellungen\P atrick\Eigene Dateien\Visual Studio
    2005\Projects\A ssignment4\Debu g\Assignment4.e xe : fatal error LNK1120:
    1 unresolved externals
    Build log was saved at "file://c:\Dokumente und
    Einstellungen\m atti\Eigene Dateien\Visual Studio
    2005\Projects\A ssignment4\Assi gnment4\Debug\B uildLog.htm"
    Assignment4 - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
    ==========

    I look for the error for many hours but I simply do not find how I can
    fix it :( Does anybody know here how I can fix this error?? :((

    Regards

  • Thomas J. Gritzan

    #2
    Re: unresolved external symbol

    pat270881 schrieb:
    hello,
    >
    I use the Visual C++ 2005 Express Edition and I want to implement a
    simple c++ programm which has a header file and a implementation file.
    [snip]

    Here the linker clearly states what he is missing:
    msvcrtd.lib(crt exe.obj) : error LNK2019: unresolved external symbol
    _main referenced in function ___tmainCRTStar tup
    You need to define a main() function:

    int main() // or:
    int main(int argc, char* argv[])

    --
    Thomas

    Comment

    • pat270881

      #3
      Re: unresolved external symbol


      okay thank you very much! unfortunately I have one more problem, I have
      now the following:

      the header file:

      Code:
      #include <iostream>
      
      namespace main_savitch_2C
      {
      class statistician
      {
      public:
      void next(double r);
      
      private:
      
      
      bool operator ==(const statistician& s1, const statistician& s2)
      {
      return true;
      }
      and my testfile:
      [code]
      #include <iostream // Provides cout, cin
      #include "stats.h"

      using namespace main_savitch_2C ;
      using namespace std;

      int main(int argc, char* argv[])
      {
      statistician s1, s2, s3;
      if (s1 == s2)
      cout << "s1 and s2 are equal." << endl;
      else
      cout << "s1 and s2 are not equal." << endl;
      }

      I know that the operator== function is not correctly implemented yet
      but I want only test the overall running of the program, I implement
      this function later. The objects s1, s2 and s3 are from statistician
      and contain above other variables a number. But when I run this
      programm I get the following error:

      ------ Build started: Project: Assignment4, Configuration: Debug Win32
      ------
      Linking...
      stattest.obj : error LNK2019: unresolved external symbol "bool __cdecl
      main_savitch_2C ::operator==(cl ass main_savitch_2C ::statistician const
      &,class main_savitch_2C ::statistician const &)"
      (??8main_savitc h_2C@@YA_NABVst atistician@0@0@ Z) referenced in function
      _main
      C:\Dokumente und Einstellungen\P atrick\Eigene Dateien\Visual Studio
      2005\Projects\A ssignment4\Debu g\Assignment4.e xe : fatal error LNK1120:
      1 unresolved externals
      Build log was saved at "file://c:\Dokumente und
      Einstellungen\P atrick\Eigene Dateien\Visual Studio
      2005\Projects\A ssignment4\Assi gnment4\Debug\B uildLog.htm"
      Assignment4 - 2 error(s), 0 warning(s)
      ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
      ==========

      Do you know how I can fix this problem? Has this something to do with
      the fact that the operator== is a non-member function??

      Please help me once more.:(

      Regards

      Comment

      • Thomas J. Gritzan

        #4
        Re: unresolved external symbol

        pat270881 schrieb:
        okay thank you very much! unfortunately I have one more problem, I have
        now the following:
        >
        the header file:
        >
        Code:
        #include <iostream>
        >
        namespace main_savitch_2C
        {
            class statistician
            {
            public:
                void next(double r);
        >
            private:
        >
        >
        bool operator ==(const statistician& s1, const statistician& s2)
        {
        	return true;
        }
        This is obviously not your real code. There are some closing curly brackets
        missing. Read this:


        --
        Thomas

        Comment

        • pat270881

          #5
          Re: unresolved external symbol



          What do you mean with that?? - This code is my real code, I try to get
          it work, okay here once more my code:

          The HEADER-File - stats.h:

          #ifndef STATS_H // Prevent duplicate definition
          #define STATS_H
          #include <iostream>

          namespace main_savitch_2C
          {
          class statistician
          {
          public:
          // CONSTRUCTOR
          statistician( );
          // MODIFICATION MEMBER FUNCTIONS
          void next(double r);
          void reset( );
          // CONSTANT MEMBER FUNCTIONS
          int length( ) const;
          double sum( ) const;
          double mean( ) const;
          double minimum( ) const;
          double maximum( ) const;
          // FRIEND FUNCTIONS
          friend statistician operator +
          (const statistician & s1, const statistician & s2);

          private:
          int count; // How many numbers in the sequence
          double total; // The sum of all the numbers in the sequence
          double tinyest; // The smallest number in the sequence
          double largest; // The largest number in the sequence
          };

          // NON-MEMBER functions for the statistician class
          bool operator ==(const statistician& s1, const statistician& s2);
          }

          #endif

          The implementation-file - statsimpl.cpp:

          #include "stats.h"
          #include <iostream>


          using namespace main_savitch_2C ;


          statistician::s tatistician()
          {
          count = 0;
          total = 0;
          tinyest = 0;
          largest = 0;
          }

          void statistician::n ext(double r)
          {
          total += r;
          std::cout << total << std::endl;
          }

          int statistician::l ength() const
          {
          return 0;
          }

          void statistician::r eset( )
          {
          count = 0;
          total = 0;
          tinyest = 0;
          largest = 0;
          }

          double statistician::s um() const
          {
          return total;
          }

          double statistician::m ean() const
          {
          return total/count;
          }

          double statistician::m inimum() const
          {
          return tinyest;
          }

          double statistician::m aximum() const
          {
          return largest;
          }

          bool operator ==(const statistician& s1, const statistician& s2)
          {
          return true;
          }

          statistician operator + (const statistician & s1, const statistician &
          s2)
          {
          statistician s3;

          s3.next(s1.sum( ) + s2.sum());

          return s3;
          }

          And when I try to test this with this testfile - statstest.cpp:

          #include <cctype // Provides toupper
          #include <iomanip // Provides setw to set the width of an output
          #include <iostream // Provides cout, cin
          #include <cstdlib // Provides EXIT_SUCCESS
          #include "stats.h"

          using namespace main_savitch_2C ;
          using namespace std;

          void print_menu( )
          {
          cout << endl;
          cout << "The following choices are available: " << endl;
          cout << " R Activate one of the reset( ) functions" << endl;
          cout << " 1 Add a new number to the 1st statistician s1" << endl;
          cout << " 2 Add a new number to the 2nd statistician s2" << endl;
          cout << " 3 Add a new number to the 3rd statistician s3" << endl;
          cout << " T Print a table of values from the statisticians" <<
          endl;
          cout << " E Test whether s1 == s2" << endl;
          cout << " + Set the third statistician s3 equal to s1 + s2" <<
          endl;
          cout << " * Set the third statistician s3 equal to x*s1" << endl;
          cout << " Q Quit this test program" << endl;
          }

          char get_user_comman d( )
          // Library facilties used: iostream.h
          {
          char command;

          cout << "Enter choice: ";
          cin >command;

          return command;
          }

          double get_number( )
          // Library facilties used: iostream.h
          {
          double result;

          cout << "Please enter the next real number for the sequence: ";
          cin >result;
          cout << result << " has been read." << endl;
          return result;
          }

          void print_values(co nst statistician& s)
          // Library facilties used: iostream.h
          {
          cout << setw(10) << s.length( );
          cout << setw(10) << s.sum( );
          if (s.length( ) != 0)
          {
          cout << setw(10) << s.minimum( );
          cout << setw(10) << s.mean( );
          cout << setw(10) << s.maximum( );
          }
          else
          cout << " none none none";
          cout << endl;
          }

          int main(int argc, char* argv[])
          {
          statistician s1, s2, s3; // Three statisticians for us to play with
          char choice; // A command character entered by the
          user
          double x; // Value for multiplication x*s1

          cout << "Three statisticians s1, s2, and s3 are ready to test." <<
          endl;

          do
          {
          cout << endl;
          print_menu( );
          choice = toupper(get_use r_command( ));
          switch (choice)
          {
          case 'R': cout << "Which one should I reset (1, 2, 3) " << endl;
          choice = get_user_comman d( );
          switch (choice)
          {
          case '1': s1.reset( );
          break;
          case '2': s2.reset( );
          break;
          case '3': s3.reset( );
          break;
          }
          cout << "Reset activated for s" << choice << "."
          << endl;
          break;
          case '1': s1.next(get_num ber( ));
          break;
          case '2': s2.next(get_num ber( ));
          break;
          case '3': s3.next(get_num ber( ));
          break;

          case '4': x = s1.sum();
          cout << "Die Summe der Zahlen beträgt: " << x << endl;

          x = s2.sum();
          cout << "Die Summe der Zahlen beträgt: " << x << endl;
          break;

          case '+': s3 = s1 + s2;
          cout << "s3 has been set to s1 + s2" << endl;
          break;

          case 'T': cout << "The values are given in this table:" <<
          endl;
          cout << " LENGTH SUM"
          << " MINIMUM MEAN MAXIMUM" << endl;
          cout << " s1";
          print_values(s1 );
          cout << " s2";
          print_values(s2 );
          cout << " s3";
          print_values(s3 );
          break;

          case 'Q': cout << "Ridicule is the best test of truth." <<
          endl;
          break;
          default: cout << choice << " is invalid. Sorry." << endl;
          }
          }
          while ((choice != 'Q'));

          return EXIT_SUCCESS;

          }

          I get the following error:

          ------ Build started: Project: Assignment4, Configuration: Debug Win32
          ------
          Compiling...
          statsimpl.cpp
          Linking...
          stattest.obj : error LNK2019: unresolved external symbol "class
          main_savitch_2C ::statistician __cdecl main_savitch_2C ::operator+(cla ss
          main_savitch_2C ::statistician const &,class
          main_savitch_2C ::statistician const &)"
          (??Hmain_savitc h_2C@@YA?AVstat istician@0@ABV1 0@0@Z) referenced in
          function _main
          C:\Dokumente und Einstellungen\m atti\Eigene Dateien\Visual Studio
          2005\Projects\A ssignment4\Debu g\Assignment4.e xe : fatal error LNK1120:
          1 unresolved externals
          Build log was saved at "file://c:\Dokumente und
          Einstellungen\m atti\Eigene Dateien\Visual Studio
          2005\Projects\A ssignment4\Assi gnment4\Debug\B uildLog.htm"
          Assignment4 - 2 error(s), 0 warning(s)
          ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
          ==========

          This is the whole code, please help me with that, I simply can't find
          the error...:((

          Regards

          matti

          Comment

          • Amit

            #6
            Re: unresolved external symbol

            Hello Matti,

            I don't have VC++ 2005 edition, so I tested your code on VC++ 2003
            edition.

            I made the following changes to get it working. Try making the same
            changes at your end.

            //Replace this function defination by the following defination
            statistician operator + (const statistician & s1, const statistician &
            s2)
            {
            statistician s3;

            s3.next(s1.sum( ) + s2.sum());

            return s3;

            }

            statistician main_savitch_2C ::operator + (const statistician & s1,
            const statistician &
            s2)
            {
            statistician s3;

            s3.next(s1.sum( ) + s2.sum());

            return s3;

            }

            I hope this helps

            Best Regards
            -Amit Gupta

            pat270881 wrote:
            What do you mean with that?? - This code is my real code, I try to get
            it work, okay here once more my code:
            >
            The HEADER-File - stats.h:
            >
            #ifndef STATS_H // Prevent duplicate definition
            #define STATS_H
            #include <iostream>
            >
            namespace main_savitch_2C
            {
            class statistician
            {
            public:
            // CONSTRUCTOR
            statistician( );
            // MODIFICATION MEMBER FUNCTIONS
            void next(double r);
            void reset( );
            // CONSTANT MEMBER FUNCTIONS
            int length( ) const;
            double sum( ) const;
            double mean( ) const;
            double minimum( ) const;
            double maximum( ) const;
            // FRIEND FUNCTIONS
            friend statistician operator +
            (const statistician & s1, const statistician & s2);
            >
            private:
            int count; // How many numbers in the sequence
            double total; // The sum of all the numbers in the sequence
            double tinyest; // The smallest number in the sequence
            double largest; // The largest number in the sequence
            };
            >
            // NON-MEMBER functions for the statistician class
            bool operator ==(const statistician& s1, const statistician& s2);
            }
            >
            #endif
            >
            The implementation-file - statsimpl.cpp:
            >
            #include "stats.h"
            #include <iostream>
            >
            >
            using namespace main_savitch_2C ;
            >
            >
            statistician::s tatistician()
            {
            count = 0;
            total = 0;
            tinyest = 0;
            largest = 0;
            }
            >
            void statistician::n ext(double r)
            {
            total += r;
            std::cout << total << std::endl;
            }
            >
            int statistician::l ength() const
            {
            return 0;
            }
            >
            void statistician::r eset( )
            {
            count = 0;
            total = 0;
            tinyest = 0;
            largest = 0;
            }
            >
            double statistician::s um() const
            {
            return total;
            }
            >
            double statistician::m ean() const
            {
            return total/count;
            }
            >
            double statistician::m inimum() const
            {
            return tinyest;
            }
            >
            double statistician::m aximum() const
            {
            return largest;
            }
            >
            bool operator ==(const statistician& s1, const statistician& s2)
            {
            return true;
            }
            >
            statistician operator + (const statistician & s1, const statistician &
            s2)
            {
            statistician s3;
            >
            s3.next(s1.sum( ) + s2.sum());
            >
            return s3;
            }
            >
            And when I try to test this with this testfile - statstest.cpp:
            >
            #include <cctype // Provides toupper
            #include <iomanip // Provides setw to set the width of an output
            #include <iostream // Provides cout, cin
            #include <cstdlib // Provides EXIT_SUCCESS
            #include "stats.h"
            >
            using namespace main_savitch_2C ;
            using namespace std;
            >
            void print_menu( )
            {
            cout << endl;
            cout << "The following choices are available: " << endl;
            cout << " R Activate one of the reset( ) functions" << endl;
            cout << " 1 Add a new number to the 1st statistician s1" << endl;
            cout << " 2 Add a new number to the 2nd statistician s2" << endl;
            cout << " 3 Add a new number to the 3rd statistician s3" << endl;
            cout << " T Print a table of values from the statisticians" <<
            endl;
            cout << " E Test whether s1 == s2" << endl;
            cout << " + Set the third statistician s3 equal to s1 + s2" <<
            endl;
            cout << " * Set the third statistician s3 equal to x*s1" << endl;
            cout << " Q Quit this test program" << endl;
            }
            >
            char get_user_comman d( )
            // Library facilties used: iostream.h
            {
            char command;
            >
            cout << "Enter choice: ";
            cin >command;
            >
            return command;
            }
            >
            double get_number( )
            // Library facilties used: iostream.h
            {
            double result;
            >
            cout << "Please enter the next real number for the sequence: ";
            cin >result;
            cout << result << " has been read." << endl;
            return result;
            }
            >
            void print_values(co nst statistician& s)
            // Library facilties used: iostream.h
            {
            cout << setw(10) << s.length( );
            cout << setw(10) << s.sum( );
            if (s.length( ) != 0)
            {
            cout << setw(10) << s.minimum( );
            cout << setw(10) << s.mean( );
            cout << setw(10) << s.maximum( );
            }
            else
            cout << " none none none";
            cout << endl;
            }
            >
            int main(int argc, char* argv[])
            {
            statistician s1, s2, s3; // Three statisticians for us to play with
            char choice; // A command character entered by the
            user
            double x; // Value for multiplication x*s1
            >
            cout << "Three statisticians s1, s2, and s3 are ready to test." <<
            endl;
            >
            do
            {
            cout << endl;
            print_menu( );
            choice = toupper(get_use r_command( ));
            switch (choice)
            {
            case 'R': cout << "Which one should I reset (1, 2, 3) " << endl;
            choice = get_user_comman d( );
            switch (choice)
            {
            case '1': s1.reset( );
            break;
            case '2': s2.reset( );
            break;
            case '3': s3.reset( );
            break;
            }
            cout << "Reset activated for s" << choice << "."
            << endl;
            break;
            case '1': s1.next(get_num ber( ));
            break;
            case '2': s2.next(get_num ber( ));
            break;
            case '3': s3.next(get_num ber( ));
            break;
            >
            case '4': x = s1.sum();
            cout << "Die Summe der Zahlen beträgt: " << x << endl;
            >
            x = s2.sum();
            cout << "Die Summe der Zahlen beträgt: " << x << endl;
            break;
            >
            case '+': s3 = s1 + s2;
            cout << "s3 has been set to s1 + s2" << endl;
            break;
            >
            case 'T': cout << "The values are given in this table:" <<
            endl;
            cout << " LENGTH SUM"
            << " MINIMUM MEAN MAXIMUM" << endl;
            cout << " s1";
            print_values(s1 );
            cout << " s2";
            print_values(s2 );
            cout << " s3";
            print_values(s3 );
            break;
            >
            case 'Q': cout << "Ridicule is the best test of truth." <<
            endl;
            break;
            default: cout << choice << " is invalid. Sorry." << endl;
            }
            }
            while ((choice != 'Q'));
            >
            return EXIT_SUCCESS;
            >
            }
            >
            I get the following error:
            >
            ------ Build started: Project: Assignment4, Configuration: Debug Win32
            ------
            Compiling...
            statsimpl.cpp
            Linking...
            stattest.obj : error LNK2019: unresolved external symbol "class
            main_savitch_2C ::statistician __cdecl main_savitch_2C ::operator+(cla ss
            main_savitch_2C ::statistician const &,class
            main_savitch_2C ::statistician const &)"
            (??Hmain_savitc h_2C@@YA?AVstat istician@0@ABV1 0@0@Z) referenced in
            function _main
            C:\Dokumente und Einstellungen\m atti\Eigene Dateien\Visual Studio
            2005\Projects\A ssignment4\Debu g\Assignment4.e xe : fatal error LNK1120:
            1 unresolved externals
            Build log was saved at "file://c:\Dokumente und
            Einstellungen\m atti\Eigene Dateien\Visual Studio
            2005\Projects\A ssignment4\Assi gnment4\Debug\B uildLog.htm"
            Assignment4 - 2 error(s), 0 warning(s)
            ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped
            ==========
            >
            This is the whole code, please help me with that, I simply can't find
            the error...:((

            Regards

            matti

            Comment

            Working...