|
I'm not a computer major student. I'm using C ++ recently and I'm fainted by a mentally handicapped include problem. Please master one or two:
The program has 3 files, main.cpp, a.h, a.cpp
The main function is in main.cpp
a.h is a header file of class a, which declares some interfaces
a.cpp is the implementation file for class a
code show as below:
//////////////////////////////////////main.cpp
#ifndef MAIN_CPP
#define MAIN_CPP
#include <iostream.h>
#include "a.h"
int main ()
{
a <int> d;
d.setdata (3);
cout << d.getdata () << endl;
return 0;
}
#endif
#ifndef A_H
#define A_H
/////////////////////////////////////////a.h
template <class type>
class a
{
public:
type data;
a () {}
~ a () {}
void setdata (type value);
type getdata ();
};
#endif
////////////////////////////////////////////////a.cpp
#ifndef A_CPP
#define A_CPP
#include "a.h"
template <class type>
void a <type> :: setdata (type value)
{
a <type> :: data = value;
}
template <class type>
type a <type> :: getdata ()
{
return a <type> :: data;
}
#endif
The question is:
This code can be passed when compiling, but there is a problem with the linking process. VC6 gives the following error
Compiling ...
main.cpp
Linking ...
main.obj: error LNK2001: unresolved external symbol "public: void __thiscall a <int> :: setdata (int)" (? setdata @? $ a @ H @@ QAEXH @ Z)
Debug / main.exe: fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
main.exe-2 error (s), 0 warning (s)
Then I copied the code in a.cpp to a.h, that is, a.h implements the declaration and definition of class a, and delete a.cpp at the same time, the program will have no problems!
why? ? ? ? ? ? ? ? |
|