Thread: questions about classes(implementation files)

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    42

    questions about classes(implementation files)

    Hi. I got a couple questions about implementation files. My book that I'm going through says to put the class in a header file, and the application and the class implementation into .cpp files(or whatever the system uses). No problem there. However, its telling me that you can compile the application file and the implementation file separately, and that the implementation file needs to be compiled only once.
    Is this some old feature of C++, or do i just need to find a way to let my compiler do it? I can't compile the implementation file because there is no main() in it. Of course, linking and then compiling the application file makes it all work. I checked my compiler options for something abou this.

    Second problem:

    In the book I have an exercise where I'm using two different classes.
    In the implementation files for both classes, I had a helping function called digit_to_int(char c). My book says these helping functions are local to the implementation files, so I could have the same function in my application file and implementation file and the compiler would regard them as different. However, my compiler complains, and I had to change one of the function names. Is this an old feature of c++ as well or something? If I had losts of classes, it would kind of suck if I had to make sure all my helping functions have different names. What should I do? Make them private member functions? Use #ifndef, ect?


    Not very important questions, but I would like to figure this out. The book is pretty old and my compiler is Dev-C++.

    Thanks.
    Last edited by Link_26; 06-21-2006 at 07:59 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I can't compile the implementation file because there is no main() in it
    No... you just can't link it together with any number of things none of which containing a main().

    Depending on your compiler, you'll have to pass a command that says compile but don't link. g++ for instance would be (-c):
    Code:
    g++ -c myImplementation.cpp
    This may be standard for all compilers, I'm not sure. Anyway, this will give you an object file that you can now link with any program that needs that particular implementation instead of recompiling the source file everytime. Welcome to your first lesson in modularity.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    It was giving me a linking error to winmain ect. So you mean I have to tell the compiler not the try to link it, right?

    I tried what you said. It did say compilation successful, but an output size of 0. I Not sure if it did anything. I'll look at the help files. Thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to add all the cpp files to your Dev-C++ project. It will compile each separately and then link them together. When it links them it will find the one (and only one) with a main(). If it complains about WinMain, perhaps you created the wrong kind of project.

    >> My book says these helping functions are local to the implementation files.
    They are only local if you make them local, otherwise they will cause errors when you link. To make them local to the file, use an unnamed namespace:
    Code:
    namespace
    {
      // your local function and/or variables here.
    }
    You could also make them static in the file, although that solution has been deprecated in favor of the unnamed namespace.

    >> If I had losts of classes, it would kind of suck if I had to make sure all my helping functions have different names.
    Using functions local to the source file isn't that common. Usually your functions are members of the class, or are specific to the class. If you have a function that is the same and you use in multiple source files, you don't want to add it to each source file, you want to prototype it in a header file and define it once (not in the unnamed namespace) in a source file.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Quote Originally Posted by Daved
    You have to add all the cpp files to your Dev-C++ project. It will compile each separately and then link them together. When it links them it will find the one (and only one) with a main(). If it complains about WinMain, perhaps you created the wrong kind of project.
    I'm not doing a dev-c++ project, but I have a program like so:

    Code:
    //small test program
    #include "dtime.h"          //digitaltime class
    #include <iostream>
    #include <ctype.h>
    #include <stdlib.h>
    using namespace std;
    
    int main()
    {
        DigitalTime current(1, 0), previous(2, 0);
        cin >> current;
        cout << current;
        system("PAUSE");
        return 0;
    }
    When I link the digitaltime implementation file, it works fine. However, my book is saying that if I change the implementation file, all I need to do is recompile it and relink, and I won't even have to recompile the above code. That's what I'm trying to figure out how to do. I can post the direct quote if you want.


    EDIT: Eh, well, even if I figured out how to compile the implementation, It doesn't look like dev-c++ can link without compiling, so I probably can't just relink. Thanks for your other help.
    Last edited by Link_26; 06-21-2006 at 10:39 PM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I have to be honest with you, I tried this with Dev-C++ and failed too.

    If you pass the -c parameter to the compiler through the IDE, for some reason Dev-C++ ignores linking, but creates an executable. What you have to do is go onto your command prompt, go to the ".\Dev-Cpp\bin" directory and compile with the g++ application.
    Code:
    g++ -c [Source File]
    This will give you a .o in that bin directory for you to link with whatever.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    Excellent. I did that and obtained the .o file. Thanks sly and dave.
    Last edited by Link_26; 06-22-2006 at 11:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  4. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM