bonjour,
j'essaie de creer un object static qui pourrait etre appele par plusieurs autres objects. Je bute apparement sur l'initialisation de cet objet car j'obtiens invariablement
[Linker error] unresolved external .... referenced from <et ici le nom de la definition de mon object statique ....
bref, je dois louper qque chose.
Est-ce que qqu'un pourrait me mettre sur la voie ?
ci-dessous les 3 petits ficheirs sources:
- unit2 qui implemente la class avec les methods statiques
- unit 3 qui implement un client de cet objet
- unit 1 un petit main()
Code Unit3.h : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 //--------------------------------------------------------------------------- #ifndef Unit3H #define Unit3H //--------------------------------------------------------------------------- class client { public: client() {} ~client() {} void process (void); }; #endif
Code Unit3.cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 //--------------------------------------------------------------------------- #pragma hdrstop #include "Unit3.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #include "unit2.h" void client::process(void) { int a = primId::getUserId(); }
Code Unit2.h : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 //--------------------------------------------------------------------------- #ifndef Unit2H #define Unit2H //--------------------------------------------------------------------------- class primId { public: static void newL (int a, int b); static int getUserId(); static int getAgentId(); static char* getKey(); private: static int userId; static int agentId; static char key[20]; }; #endif
Code Unit2.cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 //--------------------------------------------------------------------------- #pragma hdrstop #include <string.h> #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) const char *_key = "secretKey"; void primId::newL (int a, int b) { primId::userId = a; primId::agentId = b; memcpy (key, _key, sizeof(_key)); } int primId::getUserId(void) { return primId::userId; } int primId::getAgentId (void) { return primId::agentId; } char* primId::getKey(void) { return primId::key; }
Code Unit1.cpp : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 //--------------------------------------------------------------------------- #pragma hdrstop //--------------------------------------------------------------------------- #pragma argsused #include "unit2.h" #include "unit3.h" primId obj; int main(int argc, char* argv[]) { obj.newL(2,3); // primId::newL(2,3); client aClient; aClient.process(); return 0; } //---------------------------------------------------------------------------
Merci,
Jacques
Partager