Thread: How to Package a Class

  1. #1
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27

    How to Package a Class

    How is a C++ class packaged? I want to build a library of classes that I can reuse. The class definition I'm guessing goes to a header file. I suppose the class implementation (i.e., methods, etc. ) goes to a CPP file (same name?). I suppose I will next add the header to the package (i.e., source file) where I will use the class.

    I did all these, but my compiler is giving me a linker error - undefined reference (to the method I called in the main program).

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You'll need to compile and link the class that you want to reuse. For example, the cpp file could be added to a new project (the headers don't need to be part of a project as far as the compiler is told where to find them), or you could build a static library and link with that.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    Like I said, I already did these things, but I'm getting a linker error - a linker error - undefined reference to MyClass::init() from my compiler Dev-C++. I put the
    Code:
    class MyClass { ... };
    inside the header file, but I left the
    Code:
    inline void MyClass::init() { .. }
    and the rest of the methods in the CPP file.

    The class itself already compiled and ran without errors when included with the main() program. So, I'm guessing I just missed something in the MyClass.cpp.

    By the way, should the file name of the class and the class name be the same? I think that's the rule in Java if I remember correctly. Anyway, I'll test if I get a different result because right now they are different.
    Last edited by Kanshu; 05-11-2009 at 03:31 AM.

  4. #4
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    Although my intention is to build a library of classes, naturally at this stage in the design process, I won't be creating the static library yet. I'll be opening/editing/compiling both main source file and the class source file.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just to confirm: are all the headers and source files in the same Dev-C++ project?

    Quote Originally Posted by Kanshu
    By the way, should the file name of the class and the class name be the same?
    There is no need for that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    Yup. Same project.

  7. #7
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    And same folder.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the linker error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    These are the three files. I removed the implementation in the stackk.cpp, but both main.cpp and stackk.h contain all those lines.

    Code:
    // File: main.cpp
    //---------------------------
    #include <cstdlib>
    #include <iostream>
    #include "stackk.h"
    
    using namespace std;
    
    //---------------------------------------------------------
    int main( int argc, char *argv[] ) {
    //---------------------------------------------------------
    stackk a_stack;      // Stack we want to use
    
    a_stack.init(  );
    
    // Push three value on the stack
    a_stack.push(1);
    a_stack.push(2);
    a_stack.push(3);
    
    // Pop the item from the stack
    std::cout << "Expect a 3 ->" << a_stack.pop() << '\n';
    std::cout << "Expect a 2 ->" << a_stack.pop() << '\n';
    std::cout << "Expect a 1 ->" << a_stack.pop() << '\n';
    
    system("PAUSE");
    return EXIT_SUCCESS;
    }
    Code:
    // File: stackk.h
    const int STACK_SIZE = 100;     // Maximum size of a stack
    
    class stackk 
    {
     private:
       int count;              // Number of items in the stack
       int data[STACK_SIZE];   // The items themselves
    
     public:  
       void init(  ); // Initialize the stack
       void push(const int item); // Push an item on the stack
       int pop(  ); // Pop an item from the stack
    };
    Code:
    // File: stackk.cpp
    //------------------------------
    #include <cstdlib>
    #include <iostream>
    #include <assert.h>
    
    #include "stackk.h"
    
    inline void stackk::init() 
    { ... }
    
    inline void stackk::push( const int item ) 
    { ... }
    
    inline int stackk::pop() 
    { ... }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably should not declare the function inline in the .cpp file, if it's not declared inline in the header file. Further, if you REALLY want the calling code to have the function call inlined, you should move the implementation (function definition) into the header file, as there are only a few compilers that can inline functions compiled in a .cpp file - inline generally requires that the compiler "sees" the code during the compile of the calling code - main() in this case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    Actually, I don't know about the "inline" keyword so I have no preference. I just copied the code straight from the O'Reilly CHM and pasted it on my compiler. It worked fine at first because all the codes are in the main.cpp until of course, I decided to move them.

    Anyway, I removed the inline keyword and it's working now. Thanks.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, for the purpose of education: inline means that the compiler should attempt to put the code of the function INTO the calling code, instead of actually calling the function. Actually calling a function is often quite a bit of overhead, so a simple function that for example simply returns a value or adds to integers together may take 10x longer to for the call and return than the actual operation inside the function.

    The solution to this is to inline those functions. But a requirement for that to work is that the compiler actually can see the inlined function before it sees the calling code [or at least that it's called and defined in the same compile unit (that's the source code + all its includes, generally speaking)].

    Functionally, inline and out-of-line functions should have the same effect. From an optimization perspective, there will be benefits in both directions - inline code CAN speed the code up, but too aggressive inlining of large functions can lead to code-bloat and potentially slower execution speed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User Kanshu's Avatar
    Join Date
    May 2009
    Posts
    27
    Well, for the kind of applications I'm developing (not in C/C++), I probably don't need a lot of speed because me and my target users won't notice the difference in fractions of a second.

    However, there is personal project that I'm about to restart involving a lot of computation and speed. It's a lotto game simulation system. Actually, I'm about to start coding the class tree to C++. That's why I needed to start packaging my codes right from the start.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM