By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Link error.

Roonie
P: 99
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:
----------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. #include "fish.cpp"
  6.  
  7. using namespace std;
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     typedef std::vector<Fish> t_FishVect;
  12.     t_FishVect school(10);
  13.  
  14.     for(int i = 0; i < 10; i++)
  15.     {
  16.             school[i].setFace(i);
  17.     }
  18.  
  19.     for(int i = 0; i < 10; i++)
  20.     {
  21.             cout << school[i].getFace();
  22.     }
  23.  
  24.     cout << "\n";
  25.     system("PAUSE");
  26.     return EXIT_SUCCESS;
  27. }
----------------------------------------------------------------------------

fish.cpp:
----------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. class Fish
  2. {
  3.       public:
  4.              Fish() {face = 0;}
  5.              Fish(int fishFace) {face = fishFace;}
  6.              ~Fish();
  7.  
  8.              void setFace(int newFace) {face = newFace;}
  9.              int getFace() {return face;}
  10.  
  11.       private:
  12.               int face;
  13. };
----------------------------------------------------------------------------

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.
Mar 18 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 100+
P: 1,510
in Fish you declare the destructor
Expand|Select|Wrap|Line Numbers
  1.              ~Fish();
  2.  
but don't define it (give it any code enclosed in {}), try
Expand|Select|Wrap|Line Numbers
  1.              ~Fish() {};
  2.  
Mar 18 '07 #2

Roonie
P: 99
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 . . .)
Mar 18 '07 #3

arne
Expert 100+
P: 315
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.
Mar 18 '07 #4

Roonie
P: 99
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.
Mar 18 '07 #5

Expert 100+
P: 1,510
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?
Mar 18 '07 #6

Roonie
P: 99
that is exactly what im trying to get . . .

main.cpp is the same.

fish.cpp:
Expand|Select|Wrap|Line Numbers
  1. class Fish
  2. {
  3.       public:
  4.              Fish();
  5.              Fish(int fishFace);
  6.              ~Fish() {};
  7.  
  8.              void setFace(int newFace);
  9.              int getFace();
  10.  
  11.       private:
  12.               int face;
  13. };
  14.  
  15. Fish::Fish()
  16. {
  17.      face = 0;
  18. }
  19.  
  20. Fish::Fish(int fishNum)
  21. {
  22.      face = fishNum;
  23. }
  24.  
  25. void Fish::setFace(int newFace)
  26. {
  27.      face = newFace;
  28. }
  29.  
  30. int Fish::getFace()
  31. {
  32.      return face;
  33. }
  34.  
Mar 18 '07 #7

arne
Expert 100+
P: 315
what compiler are you using arne?
I am using gcc 4.0.2.
Mar 18 '07 #8

arne
Expert 100+
P: 315
A run gives
0123456789
Equivalent result for me:
0123456789
sh: PAUSE: command not found
Mar 18 '07 #9

Expert 100+
P: 1,510
that is exactly what im trying to get . . .

main.cpp is the same.

fish.cpp:
Expand|Select|Wrap|Line Numbers
  1. class Fish
  2. {
  3.       public:
  4.              Fish();
  5.              Fish(int fishFace);
  6.              ~Fish() {};
  7.  
  8.              void setFace(int newFace);
  9.              int getFace();
  10.  
  11.       private:
  12.               int face;
  13. };
  14.  
  15. Fish::Fish()
  16. {
  17.      face = 0;
  18. }
  19.  
  20. Fish::Fish(int fishNum)
  21. {
  22.      face = fishNum;
  23. }
  24.  
  25. void Fish::setFace(int newFace)
  26. {
  27.      face = newFace;
  28. }
  29.  
  30. int Fish::getFace()
  31. {
  32.      return face;
  33. }
  34.  
this compiles and runs with the original main() OK using g++
Mar 18 '07 #10

Roonie
P: 99
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.
Mar 18 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.