Thread: Problem linking files in C++

  1. #1
    Registered User
    Join Date
    Mar 2002

    Problem linking files in C++

    I wrote a simple bit array class declared in BitArray.h and defined in BitArray.cpp. I used G++ 2.95.3 20010315 (SuSE) to compile it. When compiled with -DDEBUG a small main function was also compiled, and everything was fine.

    However when I use it in programs written in other files, the linker can't find any of the methods, e.g.

    #include "BitArray.h"
    #include <stdlib.h>
    #include <iostream.h>

    int main (int argc, char** argv)
    BitArray b(1024); // 1024 bit array

    // print bit (0 or 1) at index 0
    cout << b.bitAt (0) << endl;

    // find index of first occurence of bit
    // 1 starting at index 0
    cout << b.findBit (0, 1) << endl;

    return EXIT_SUCCESS;

    When I compile with the following
    g++ -c BitArray.cpp
    g++ test.cpp BitArray.o

    I get:
    /tmp/cc31oTBq.o: In function `main':
    /tmp/cc31oTBq.o(.text+0x2f): undefined reference to `BitArray::bitAt(unsigned int) const'
    collect2: ld returned 1 exit status

    However the exact same code in the main function in BitArray.cpp works.

    Can anyone tell me what's going on?!!

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Dunno... if you post the code I'll try to help you get it to link though.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Sep 2001
    Do you use a constructor?

  4. #4
    Registered User
    Join Date
    Mar 2002


    Thanks lads, but in the end I just re-wrote it in Java. There were too many weird problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Linking Files
    By MT_Pockets in forum C++ Programming
    Replies: 8
    Last Post: 02-24-2006, 08:50 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM