Thread: undefined reference

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    undefined reference

    I've just managed to compile a class I called CString
    When I try to implement this class in a program I get
    C:\WINDOWS\TEMP\ccJwtegb.o(.text+0x2a):test1.cpp: undefined reference to `CString::CString(char const *)' error. I have no idea what this means, Im guessing it has something to do with the linker but other than that Im lost.

    Using this to test my CString :
    Code:
    #include <iostream>
    using namespace std;
    #include "CString.h"
    
    int main()
    {
     CString text;
     return 0;
    }
    Appreciate any response.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I would need to see the CString.h file.

    My guess would be that you define a constructor that takes a string as an argument, but you never actually implemented the code for such a constructor.
    Best Regards,

    Bonkey

  3. #3
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    are you sure you're linking your project with the cstring
    sourcefile, ie cstring.cpp(or something like that) ?

    /btq
    ...viewlexx - julie lexx

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I'm not sure, but I think a CString may be Microsoft's own version of a string object (anybody know?) and you probably need to make sure your project includes the MFC library's. You should probably stay away from them if possible, its better to learn string objects vs CString objects if you have a choice.
    "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
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Header file
    Code:
    #ifndef CString_H
    #define CString_h
    #include <iostream>
    
    using namespace std;
    
    class CString
    {
     public:
        int main( void);
        CString( void );
        CString(const char* string);
        ~CString();
        int GetLength();
        bool IsEmpty();
        void MakeEmpty();
     private:
        int length(const char* p);
        char *pstring;
        int m_iSize;
    
    };
    #endif

    Source file:
    Code:
    #include "CString.h"
    
    using namespace std;
    
    int main( void ){return 0;}
    
    CString::CString( void ) {m_iSize=0;}
    
    CString::CString(const char* string)
    { m_iSize = length(string); }
    
    CString::~CString()  {pstring=0; delete pstring;}
    
    int CString::GetLength()  {  return m_iSize; }
    
    bool CString::IsEmpty()   {  return (m_iSize==0); }
    
    void CString::MakeEmpty() {  m_iSize=0; pstring=0;}
    
    int CString::length (const char* p) { }
    The Cstring.h and CString.cpp compile ok.

    Then I use this very simple program to test the class:
    Code:
    using namespace std;
    
    #include "CString.h"
    
    int main()
    {
     CString text;
     return 0;
    }
    In this file I get the above mentioned errors.

    Thanks for your response.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I would remove this line from CString.cpp

    int main( void ){return 0;}


    I will keep looking for your problem. This isn't it, but may cause other problems.

    also, you define this function in your CString class. It apears to be unnecessary and should be removed. If you meant to have it the change the above line to:

    int CString::main( void ){return 0;}
    Last edited by bonkey; 10-23-2002 at 11:23 AM.
    Best Regards,

    Bonkey

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    I get a weird error if I leave int main() out - undefined reference to `WinMain@16' - according to Dev-C++ faq inserting int main resolved it. So I doubt thats the solution.

    btw, using win98 and Dev-c++ 4.0

    Btw, can anyone recommend any other free ide software for c++ ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM