-3

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;
}
Stephane
  • 29
  • 1
  • 6
  • hi, That is why first i need to find the reference to the object in question by findbymap(int ) . But it seems like it doesn't get that :) – Stephane Mar 05 '17 at 23:45
  • `commissionfixed` is a member function , so you have to call it on an object instance, e.g. `val->commissionfixed(val);`. – M.M Mar 06 '17 at 00:07
  • The same also applies for `commissionextra()`. – Justin Time - Reinstate Monica Mar 06 '17 at 00:07
  • The object instance is nothing more than a pointer when they are created. There is no specific name for every instance . That is why I need to use a map to find them back when I need them. – Stephane Mar 06 '17 at 00:49

1 Answers1

0
#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;

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]; } 

void commissionfixed (){ 
commissievast = this->aantalpc*winstperpc; } 

void commissionextra (){ 
if (this->aantalpc>gemiddeldeverkoop){ commissieplus = (this->aantalpc - gemiddeldeverkoop) * 37; } } 

void readCommission(){

cout << "comission payment is " << this->commissievast << endl;
} 

static const int winstperpc; 
static int gemiddeldeverkoop;
 const int ID; 

string name;
protected: 
int aantalpc; 
int commissievast; 
int commissieplus;
 };

map<int, vertegenwoordiger*>vertegenwoordiger::registryMap;

const int vertegenwoordiger::winstperpc = 150; int vertegenwoordiger::gemiddeldeverkoop = 0;

 int main() { 

 for (int i=0; i<2; 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 << "next please : " << endl; } 

 for (int i=1; i<3 ; i++){ 

vertegenwoordiger* val = vertegenwoordiger::findbymap(i); 
val->commissionfixed ();
val->commissionextra ();

val->readCommission ();
} 


return 0; }
Stephane
  • 29
  • 1
  • 6