Thanks for the links.
Let me show you with a small int-example what I'm trying to do/accomplisch.
int is later on replaced with a record, and MYCLASS is extended with all kinds of getters/setters for the specific record ... so please don't care/react on the int.
#include <iostream>
usingnamespace std;
#include "sourch"
usingnamespace nsp_MYCLASS;
int main (int argc, char* argv[]) {
MYCLASS a (2);
}
The compiles of SOURC and SOURM go oke. But the linker is giving an error :
**** ERROR **** [1210]:
SOURM: In function `main':
SOURM(.text._168869328+0x182): unresolved reference to
__ct__Q2_11nsp_MYCLASS7MYCLASSFi (C++ mangling of
nsp_MYCLASS::MYCLASS::(int)).
What am I doing wrong ? How to get above example working ?
1) Implementation should be in same namespace as definition
2) You should not double-declare class (you didn't include header into implementation, so it won't cause errors here)
3) (In your case) Declarations should be identical: class MYCLASS and class MYCLASS : public clsC<int> is two distinct classes.
You probably can do:
1 2 3 4 5 6 7
class MYCLASS; //Forward declaration
int main()
{
MYCLASS* a = new MYCLASS; //Pointer members are allowed to use
//Without full declaration of class
}
Following on from what MiiNiPaa said, it looks like you mean?
Where SOURH, SOURM are as before...
SOURC
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
usingnamespace std;
#include "sourch"
// provide missing implementation of constructor
nsp_MYCLASS::MYCLASS::MYCLASS(int argInt)
: clsC<int>(argInt)
{
}
SOURCH
1 2 3 4 5 6 7 8 9 10 11 12
#include "sourh" // must see base class declaration
namespace nsp_MYCLASS {
class MYCLASS : public clsC<int> // and derive from it
{
private:
public:
MYCLASS(int number);
};
}
SOURM
template arg: 2
Press any key to continue . . .
Or were you trying to do something else?
Andy
PS @MiiNiPaa while you can pass pointers to objects around without seeing the class declaration, the declaration is required for construction -- as the compiler must be told about the memory layout of the class -- and use of the class -- so the function signature is known.
just imports the symbols (i.e. the various names) into the global namespace, so they can be used without having to qualify them with the name of the namespace.
But an implementation be actually in the namespace, so must either include the namespace like above
(When I use this second approach, I don't indent the classes wrt to the namespace. And I always comment the end of the namespace, so I don't loose it.)