I am trying to write a class with a map to keep a registry with unique ID for later accessing the objects. All compiled fine till i wrote the for loop in main trying to access objects of the class and their data. I am at a loss what is exactly wrong. I declared all static but doesn't work. I have been trying for several hours now but couldn't solve it. I know the problem is in the map as that is new for me, but i can't seem to find the issue. Hope someone sees what is wrong in my code.
#include <iostream>
#include <map>
#include <cassert>
#include <string>
#include <algorithm>
using namespace std;
class vertegenwoordiger{
public:
vertegenwoordiger(int id, string x, int y): ID(id), name(x),aantalpc(y) {
addtoregistry(this);
cout << "Vertegenwoordiger " << x << " is aangemaakt met " << y << " aantal verkochte pc's " << endl;
gemiddeldeverkoop = (gemiddeldeverkoop + y) / id;
}
static map<int, vertegenwoordiger*>registryMap; // PROBLEM HERE I GUESS
static void addtoregistry(vertegenwoordiger* object){
registryMap[object->ID] = object;
}
static void removefromregistry(vertegenwoordiger* object){
registryMap.erase(object->ID);
}
static vertegenwoordiger* findbymap(int id){
return registryMap[id];
} // MAYBE THIS FUNCTION IS NOT CORRECT ASWELL????
void commissionfixed (vertegenwoordiger* obj){
commissievast = obj->aantalpc*winstperpc;
}
void commissionextra (vertegenwoordiger*obj){
if (obj->aantalpc>gemiddeldeverkoop){
commissieplus = (obj->aantalpc - gemiddeldeverkoop) * 37;
}
}
static const int winstperpc;
static int gemiddeldeverkoop;
const int ID;
protected:
string name;
int aantalpc;
int commissievast;
int commissieplus;
};
const int vertegenwoordiger::winstperpc = 150;
int vertegenwoordiger::gemiddeldeverkoop = 0;
int main()
{
for (int i=0; i<4; i++){
string naam;
int pc;
cout << "geef naam in :";
cin >> naam;
cout << "geef aantal pc op :";
cin >> pc;
vertegenwoordiger* test = new vertegenwoordiger (i+1,naam,pc);
cout << "volgende aub : " << endl;
}
for (int i=1; i<4 ; i++){
vertegenwoordiger* val = vertegenwoordiger::findbymap(i); // I GUESS THE PROBLEM IS RELATED TO THIS LINE
vertegenwoordiger::commissionfixed (val);
vertegenwoordiger::commissionextra (val);
}
return 0;
}