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. ^_^