Storing pointers to dynamic memory in a <map>

I'm running into some problems storing pointers to objects I've allocated in dynamic memory with a key stored created in stack memory (relevant code only included) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static std::map<int,Interface_PhidgetModule> phidgetModules; // declared in header file


int PhidgetManager::attachHandler(CPhidgetHandle phid, void* userPtr){
        int serialNo,returnCode;
        int* serialNumPtr = &serialNo;
        int* const sptr = &serialNo;
        Interface_PhidgetModule* a;
        Interface_PhidgetModule* const ptrConstA = a;
	a = new PhidgetAccelerometer(phid);
                a->createHandle();
                returnCode = a->getSerialNumber(serialNumPtr);
                //returnCode = a->getSerialNumber(phid, serialNumPtr);
         // Insert the new Interface_PhidgetModule into phidgetModules container
        if (returnCode == 0){


            phidgetModules.insert(pair<serialNo,ptrConstA>); //<== PROBLEM HERE
        }
...
}


The compiler reports an error as :

phidgetmanager.cpp:106: error: ‘sptr’ cannot appear in a constant-expression
phidgetmanager.cpp:106: error: ‘ptrConstA’ cannot appear in a constant-expression
phidgetmanager.cpp:106: error: template argument 1 is invalid
phidgetmanager.cpp:106: error: template argument 2 is invalid

Just how am I supposed to make something constant ? Surly in this instance I am passing by value not reference ?
you don't put the variable names as template args, you put the types as template args, and the variables in the ctor list:

 
phidgetModules.insert( pair<int,Interface_PhidgetModule*>(serialNo,ptrConstA) );


Or the template args might be able to be implied:

 
.insert( pair(serialNo,ptrConstA) );


Or you could use the easy way of inserting with the [] operator:

 
phidgetModules[ serialNo ] = ptrConstA;
Thanks Disch,

But I've changed it to this :

1
2
3
4
5
6
//In the header file
typedef std::map<int,Interface_PhidgetModule*> phidgetModulesMap;
    static phidgetModulesMap phidgetModules;

//In the .cpp file
PhidgetManager::phidgetModules[ serialNo ] = ptrConstA;


But now get this :

undefined reference to `PhidgetManager::phidgetModules'

AAARRRGGGHH !

What can't it find in the header file ???
Add a line in PhidgetManager's .cpp that defines phidgetModules (all static members have to be defined, not just declared): phidgetModulesMap PhidgetManager::phidgetModules;
helios,

Isn't that what this does :

1
2
static phidgetModulesMap phidgetModules;


..which is in the header file ?
That's the declaration. The definition goes outside the class declaration. Don't put it in the header or you may get duplicate definition errors.
OK I just don't get this. Here are all the files with the relevant code only :

phidgetmanager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace phidget{
    #include <phidget21.h>
}
#include <cstdio>
#include <map>
#include "interface_phidgetmodule.h"
#include "phidgetaccelerometer.h"

class PhidgetManager {
public:
    PhidgetManager();
    PhidgetManager(const PhidgetManager& orig);
    virtual ~PhidgetManager();

    static int attachHandler(CPhidgetHandle phid, void *userPtr);
    

private:

    typedef std::map<int,Interface_PhidgetModule*> phidgetModulesMap;
    //PhidgetManager::phidgetModulesMap phidgetModules;
    static PhidgetManager::phidgetModulesMap ph;

};


phidgetmanager.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <phidget21.h>


#include "phidgetmanager.h"


//using namespace phidget;
using namespace std;
PhidgetManager::PhidgetManager() {
    // Default constructor
        hManager = 0;
        // Set debugging data on
	CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);

	//create the Manager object
	CPhidgetManager_create(&hManager);

	//Set the handlers to be run when the device is plugged in or opened from software, unplugged or closed from software, or generates an error.
	CPhidgetManager_set_OnAttach_Handler(hManager, PhidgetManager::attachHandler, hManager);
	CPhidgetManager_set_OnDetach_Handler(hManager, PhidgetManager::detachHandler, hManager);
	CPhidgetManager_set_OnError_Handler(hManager, PhidgetManager::errorHandler, NULL);
        
}

PhidgetManager::PhidgetManager(const PhidgetManager& orig) {
}

PhidgetManager::~PhidgetManager() {
    /*^*^*^*^^*^*^*^*^*^*^*^*^^*^*^*^*^*^*^*^*^*^^*^*
     * Clean up on close - empty the phidgetModule map
     **^*^*^^*^*^*^*^*^*^*^*^*^*^^*^*^*^*^*^*^*^*^^*^*^*/
}

int PhidgetManager::attachHandler(CPhidgetHandle phid, void* userPtr){
        int serialNo,returnCode;
        int* serialNumPtr = &serialNo;
        int* const sptr = &serialNo;
	const char *name;
	CPhidget_DeviceID id;
	CPhidget_DeviceClass cls;

        phidgetModulesMap mehtodscope_ph;

        //PhidgetModuleHandlesStruct hndPhids;
        Interface_PhidgetModule* a;
        Interface_PhidgetModule* const ptrConstA = a;

	
       
                a = new PhidgetAccelerometer(phid);
                a->createHandle();
                returnCode = a->getSerialNumber(serialNumPtr);
                
	

	// Insert the new Interface_PhidgetModule into phidgetModules container
        if (returnCode == 0){
            //PhidgetManager::phidgetModules.insert(pair<int,Interface_PhidgetModule*>(serialNo,ptrConstA));
            PhidgetManager::ph[ serialNo ] = ptrConstA; //<== this causes problems, Class scope ph 
                  methodscope_ph[serialNo] = ptrConstA; //<=======This works, method scope 
        }


the problem now is the class scope ph is not working giving the compiler error :

...../phidgetmanager.cpp:108: undefined reference to `PhidgetManager::ph'

I've made another schoolboy error somewhere I know.. anyone put me on the right track ?
Line 22 doesn't look anything like what I put in my other post (phidgetModulesMap PhidgetManager::phidgetModules;), and I also said it had to be outside the class declaration.
OK I've done this in phidgetmanager.h :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Definition (?) outside the class

typedef std::map<int,Interface_PhidgetModule*> phidgetModulesMap;

class PhidgetManager {
public:
    PhidgetManager();
    PhidgetManager(const PhidgetManager& orig);
    virtual ~PhidgetManager();

    ...

private:
    ...
   // Decleration(?) inside the class
    static phidgetModulesMap ph;

};




.. and the compiler still can't find it, yet if I declare a phidgetModulesMap locally, that decleration is found

Am I getting definition and decleration confused ? It's late and this has been troubling me all day.
Topic archived. No new replies allowed.