Thread: File Linking Problem

  1. #61
    Yin
    Guest
    Originally posted by Betazep
    class abc {
    private:
    int x;
    public:
    abc();
    int getx();
    };

    you got it....

    The thing is... who cares about the structure of the class? It means nothing. Just a bunch of lines that state which functions can be utilized. (and your class implementor needs to know that anyway... how can he/she call a public member function if the name and argument list isn't available? He/she can't...)

    This is why we hide the implementation, but give them the function prototypes in the header file...

    Take care... look forward to working through more issues with you in the future....

    Well, the structure of class may lead to some hints to my friend's homework. Therefore it's perfect to have it hidden too.

    And you asked "how can he/she call a public member function if the name and argument list isn't available? ". In most case you are right. But you know, this is a piece of homework. So, everyone in my class know how to implement the class.

    Nice to chat/work with you. You are really helpful.

  2. #62
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    are you guys using the same function names though?

    if you aren't then it wouldn't matter about the implementation. Nobody would be able to call a function...

    if I do not know that

    int abc(int);

    is a member of the TEST class, then I wouldn't know I could use

    TEST testing;

    i = testing.abc(10);

    it could be

    i = testing.xyz(10);

    so you have to know the function names and their arguments... else you are lost, and the class cannot be utilized.
    Blue

  3. #63
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I feel honoured to see my name up there

    Some general comments: The right way is doing it through header files. Create a headerfile for your class and include it in your main file. This way, your function prototypes are open to view for everyone, but if you are going to call them, they are known anyway. The implementation should be in your cpp file, which can be compiled into an object file. This is the standard way.

    To clarify some things that were said:

    extern

    The extern keyword is for making known variables that were declared elsewhere. However, the type of variable must be know at this time. You cannot 'extern' types, only instances of known types.

    There are ways around it. You can 'forward' a class or struct. This is what you want. You want to let your compiler know there exists a class ABC somewhere else. Forwarding a class or struct will let you use the type of this class as a typename, but not more.

    Example:

    class A;

    // so called forwarding A, compiler knows there is an A, but has
    // no clue what it might be able to do

    class B
    {
    A member; // legal
    };

    However, if you want to do anything with the member, i.e. calling functions, initialize it etc, you have to provide a full declaration of it. This is useful for classes that somehow link to each other:

    class A; // forward

    class B
    {
    A a;
    };

    class A // real declaration
    {
    B b;
    };

    Any class functions dealing with the A member of B have to be written after the complete declaration of A, or they will flag syntax errors.


    Conclusion: There is no way around, you need header file(s).



    While things may seem logical for us, once you write your own compiler or interpreter, you will notice that for the program, it's not so obvious. Something that isn't 100% sure and error free is as good as a wild guess. And guesses lead to errors. So while it would be a good guess to say you are declaring a class with this extern and as you as the user have to know better, this has to have the function you called on it, the compiler can only hope this is right. It has no way to check, at least not in this version. And hoping for you not to make errors is not in the limits of a compiler And I'm damn glad it is this way, for many of my errors would go unnoticed until the program crashes if it were the other way round
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #64
    Yin
    Guest

    Thumbs up

    Originally posted by Betazep
    are you guys using the same function names though?

    if you aren't then it wouldn't matter about the implementation. Nobody would be able to call a function...

    You are right. But we guys are really using the same function name. If not, my teacher would need to create a different file to test against each of our class. In order to reduce his workload, we are required to use to same class name, same member function name. Of course, for those extra member function we added in the class, the name is free for us to choose, but they won't be tested by my teacher.

    And what I dont want to show to my friend is the name of the member functions. Because what he need to test is only one or two required member function. But then all the member function name is visible to him. But never mind, I think the memthod of "three file" is secure enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Long file linking problem
    By hypertension in forum C Programming
    Replies: 3
    Last Post: 10-15-2002, 09:55 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM