Link error.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Roonie
    New Member
    • Mar 2007
    • 99

    Link error.

    so i signed up this morning and posted an introduction. i mentioned an error that was and then was no longer . . . thus it ceased to be a problem . . .

    but i guess i spoke too soon.

    anyways, i have not been programming for a while (six years) and this is the first time i am using a container class . . . i wrote a very simple program to test out the vector container:

    main.cpp:
    ----------------------------------------------------------------------------
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    
    #include "fish.cpp"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        typedef std::vector<Fish> t_FishVect;
        t_FishVect school(10);
        
        for(int i = 0; i < 10; i++)
        {
                school[i].setFace(i);
        }
        
        for(int i = 0; i < 10; i++)
        {
                cout << school[i].getFace();
        }
        
        cout << "\n";
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    ----------------------------------------------------------------------------

    fish.cpp:
    ----------------------------------------------------------------------------
    Code:
    class Fish
    {
          public:
                 Fish() {face = 0;}
                 Fish(int fishFace) {face = fishFace;}
                 ~Fish();
                 
                 void setFace(int newFace) {face = newFace;}
                 int getFace() {return face;}
          
          private:
                  int face;
    };
    ----------------------------------------------------------------------------

    everything compiles, but the linker exits with errors:

    [Linker error] undefined reference to 'Fish::~Fish()' (x3)

    now when i create a container of pointers to Fish it works. but as i understand it, the whole point of containers is to get away from pointers and other such nasties, so i consider a container of pointers to be a failure at an attempt to ease the use of pointers.

    as i said, ive never used a container before and im a bit rusty on c++ in general (having not used it for 6 years) so if im missing something simple, please let me know . . . earlier i cut out, saved and then pasted back the exact same portion of code (the Fish class declaration) and it worked. ill try that again, but id rather find a solution that didnt involve my linker being in a good or bad mood.

    maybe i just need a different one. i would rather not do that though . . . im using the dev-cpp ide which uses gcc and i like open source.

    any help would be greatly appreciated.
    Last edited by horace1; Mar 18 '07, 04:26 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    in Fish you declare the destructor
    Code:
                 ~Fish();
    but don't define it (give it any code enclosed in {}), try
    Code:
                 ~Fish() {};

    Comment

    • Roonie
      New Member
      • Mar 2007
      • 99

      #3
      trying . . .

      . . . same errors. i dunno. i think ill use a different compiler just to see . . .

      (also, thx for the code tags . . . didnt see that on first glance . . .)

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #4
        Originally posted by Roonie
        trying . . .

        . . . same errors. i dunno. i think ill use a different compiler just to see . . .

        (also, thx for the code tags . . . didnt see that on first glance . . .)
        Compiles fine for me with horace1's correction.
        Without it, I see the undefined reference errors you mentioned.

        Comment

        • Roonie
          New Member
          • Mar 2007
          • 99

          #5
          what compiler are you using arne?

          also, when i changed the Fish function declarations to the more appropriate style for larger functions (with prototypes and then Fish::_____'s) . . . i got additional link errors complaining that each function was defined more than once.

          im beginning to think that i just need a different compiler.

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            I have compiled and run your code with the GNU g++ compiler and Borland CBuilder without problems (after I fixed the destructor). A run gives
            0123456789
            Press any key to continue . . .

            post your updated code?

            Comment

            • Roonie
              New Member
              • Mar 2007
              • 99

              #7
              that is exactly what im trying to get . . .

              main.cpp is the same.

              fish.cpp:
              Code:
              class Fish
              {
                    public:
                           Fish();
                           Fish(int fishFace);
                           ~Fish() {};
                           
                           void setFace(int newFace);
                           int getFace();
                    
                    private:
                            int face;
              };
              
              Fish::Fish()
              {
                   face = 0;
              }
              
              Fish::Fish(int fishNum)
              {
                   face = fishNum;
              }
              
              void Fish::setFace(int newFace)
              {
                   face = newFace;
              }
              
              int Fish::getFace()
              {
                   return face;
              }

              Comment

              • arne
                Recognized Expert Contributor
                • Oct 2006
                • 315

                #8
                Originally posted by Roonie
                what compiler are you using arne?
                I am using gcc 4.0.2.

                Comment

                • arne
                  Recognized Expert Contributor
                  • Oct 2006
                  • 315

                  #9
                  Originally posted by horace1
                  A run gives
                  0123456789
                  Equivalent result for me:
                  0123456789
                  sh: PAUSE: command not found

                  Comment

                  • horace1
                    Recognized Expert Top Contributor
                    • Nov 2006
                    • 1510

                    #10
                    Originally posted by Roonie
                    that is exactly what im trying to get . . .

                    main.cpp is the same.

                    fish.cpp:
                    Code:
                    class Fish
                    {
                          public:
                                 Fish();
                                 Fish(int fishFace);
                                 ~Fish() {};
                                 
                                 void setFace(int newFace);
                                 int getFace();
                          
                          private:
                                  int face;
                    };
                    
                    Fish::Fish()
                    {
                         face = 0;
                    }
                    
                    Fish::Fish(int fishNum)
                    {
                         face = fishNum;
                    }
                    
                    void Fish::setFace(int newFace)
                    {
                         face = newFace;
                    }
                    
                    int Fish::getFace()
                    {
                         return face;
                    }
                    this compiles and runs with the original main() OK using g++

                    Comment

                    • Roonie
                      New Member
                      • Mar 2007
                      • 99

                      #11
                      alright . . . i cant seem to figure this out with the time i have right now. (my drunkie sister is going camping tonight and needs me to make a run for her).

                      thanks for the help guys . . . i guess my plan at the moment is to take a breather for a while and then try just straight up the newest release of gcc.

                      and if that fails, ill try a different container class.

                      thanks again.

                      Comment

                      Working...