Thread: File Linking Problem

  1. #1
    Yin
    Guest

    Question File Linking Problem

    First, let me provide some background information. I use g++ compiler in the cygwin shell to Program c++.

    Now, I have two files, namely, first.cc and second.cc


    The content of first.cc is:

    class abc {
    private:
    int x=99;
    public:
    int get_x() { return x; }
    };

    The content of second.cc is:

    #include <iostresm.h>
    extern class abc test_abc;

    int main() {
    cout << test_abc.get_x() << endl;
    return (0);
    }


    The expected result is screen output 99.
    There is no problem when I compile the first file into an object file.
    However, when I try to compile the second.cc (by g++ -c -o second.o second.cc), an error message " no matching function for call to 'abc:: get_x ()' " showed up.

    What irritates me is that I have already defined the class object test_abc as an extern, c++ should know that its member function get_x() belongs to a foreign file. So the error message did not make sense.

    I have tried to change the line from "extern class abc test_abc;" to "extern class abc;". And then I declare test_abc within main(). Hoever, this time the error message is " extern is only for objects or functions ". Obviously, extern did not work for class itself.

    So, what have I do wrongly? extern should support class object (class object is a kind of varible, too!). Then why c++ don't know that the object's member function belong to a foreign file as well?

    What should I do? Can anyone please help? Thanks. ^_^

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    I don't have a compiler right now to test that on, but any time I've declared an instance of a class, I've just said:
    classname objectname;
    which, in your case, would be:
    abc testabc;
    I've never used the 'extern class' command or whatever it is, and I don't know what it's meant for, but I think your second.cc might run if you tried that..
    But then again the files I've been dealing with have all been .cpp files, maybe it has to do with that.. I'm not sure, but try just saying abc testabc; and see what happens..


    Linette

  3. #3
    Yin
    Guest

    Exclamation Amendment and Results

    Firstly, Some admendment in first.cc must been make before it is compiled to object file.

    Now, first.cc is:

    class abc {
    private:
    int x;
    public:
    abc(void) { x=99; }
    int get_x() { return x; }
    };

    This is because : ANSI C++ forbids initialization of member 'x'

    And I followed Linette's advice to change my second.cc to:

    #include <iostream.h>
    abc test_abc;

    int main() {
    cout << test_abc.get_x() << endl;
    return (0);
    }


    This generated an error message when compiling:
    " 'test_abc' undeclared (first use this function) "

    If I put the 'declaration' inside main():

    #include <iostream.h>

    int main() {
    abc test_abc;
    cout << test_abc.get_x() << endl;
    return (0);
    }


    another similar error occured:
    " 'abc' undeclared (first use this function)
    'test_abc' undeclared (first use this function) "

    SO... what should I do? Help =(

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    you aren't getting an error for initializing the value of x within the class? Should be done during construct...


    ***edit... ahh, see you have gotten that far. Use a header file to declare your class and put your implementation in a seperate .cpp file. Place #include "your_h_file.h" in each cpp file. Compile both your implementation cpp file and your main cpp file together.
    Last edited by Betazep; 03-09-2002 at 11:15 PM.
    Blue

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    This may help you... GL

    Code:
    
    file.h
    
    //++++++++++++++
    
    #ifndef CLAS
    #define CLAS
    
    
    class Clas
    {
    
      public:
    
                 void hello(void);
    
    };//end class
    
    #endif
    
    //+++++++++++++++
    
    file.cpp
    
    //+++++++++++++++
    
    #include "file.h"
    #include <iostream>
    using namespace std;
    
    void Clas::hello(void)
    {
       cout << "Hello" << endl;
    }
    
    //+++++++++++++++
    
    driver.cpp
    
    //+++++++++++++++
    
    #include "file.h"
    int main()
    {
    
      Clas umm;
    
      umm.hello();
    
    return 0;
    
    }
    
    //++++++++++++++++
    Blue

  6. #6
    Yin
    Guest

    Cool

    To Betazep,

    absolutely no error for this (first.cc):

    class abc {
    private:
    int x;
    public:
    abc(void) { x=99; }
    int get_x() { return x; }
    };

    Because x=99 ins put inside the constructor of the class abc.

    P.S. Any idea of how to solve my problem?
    I still cannot find any method of compiling my second.cc into an object file... ...

  7. #7
    Yin
    Guest
    To Betazep again,

    Thanks for your help. However, the problem is that I don't want to use a header file to do this.

    I wish that I could combine the two object file, first.cc containing the class definition, second.cc containing the main program utilizing the class in first.cc .

    However, whenever I tried to compile the second.cc, error occurs.

    I donno why even I have declared the class as an extern in second.cc, the compiler still claim that it cannot find the class' member function! The compiler *should* understand that if the class is an extern, its member function is an extern as well (i.e., in a foreign file).

    =(

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    I don't know if there's a different way you're supposed to do these things with .cc files, but as far as I know, like betazep said, you should save your first.cc file as a .h file, and then include it in your second file.
    I also noticed you'd mispelled iostream. Change that too, if that was how you wrote it in your compiler (little mistakes cause hard-to-detect problems).. oh, and do leave that declaration in your main.
    That's all I can think of right now.. hope it helps.

    Linette

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    Oh, I guess what I just wrote was useless then.
    How can you use two different files, and not have one be a header file but still use the class in the other file? I think that is why you're getting that error.. I can't think of a way your compiler would know which class you want to use, without an include statement..

    ?

  10. #10
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    by using extern
    Blue

  11. #11
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    #include <iostream.h>

    int main() {
    extern abc test_abc;
    cout << test_abc.get_x() << endl;
    return (0);
    }

    Try that... I do not have time to test it out. If that doesn't work, make the class a _c style name type and extern that as

    extern _abc test_abc
    Blue

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    71
    hmm, I didn't know that..
    So why did you suggest a .h file too?

  13. #13
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    ANSI

    and I prefer it, but he doesn't want to use one. So he needs to let the compiler know that the class declaration is external to his code.
    Blue

  14. #14
    Yin
    Guest
    Originally posted by Betazep
    #include <iostream.h>

    int main() {
    extern abc test_abc;
    cout << test_abc.get_x() << endl;
    return (0);
    }

    Try that... I do not have time to test it out. If that doesn't work, make the class a _c style name type and extern that as

    extern _abc test_abc
    Well, writing "extern abc test_abc;" within main cause the following error when compiling:
    " warning: ANSI C++ forbids declaration of 'abc' with no type
    'test_abc' undeclared (first use this function) "

    Therefore I rewrite the sentence as:
    " extern class abc test_abc; "
    This time the following error occur:
    " no matching function for call to 'abc::get_x ()' "

    Your second suggest create the same error, " _abc " seems to have no different with " abc ".

    I really dunno what's wrong with the extern syntax. It is supposed to declare foreign object or function. By this way, the compiler should not try to look for member function of the foreign class object abc within second.cc . Because the compile should know that any member functions of a foreign class object belongs to a foreign file too.

    Any method? =(

  15. #15
    M.shawan
    Guest

    Thumbs up [email protected]

    class abc
    {
    private:
    int x; // you have not initial x direct with 99.

    public:
    abc() { x=99;} // constrctur initial the member x with 99.

    int get_x() { return x; }
    };

    //The content of second.cc is:

    #include <iostream.h> // syntax error"iostresm"
    extern class abc test_abc;

    int main() {

    abc test_abc; // create an test_abc object.

    cout << test_abc.get_x() << endl;

    return (0);

    }

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