I have a header file Customer.h and a source file Customer.cpp.
The Header file is as follow:
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "GeoPosition.h"
class Customer{
private:
std::string name;
std::string address;
int pin;
GeoPosition position;
public:
static int anzahl;
Customer();
Customer(std::string name, std::string address, float lat, float lon);
void getPosition();
void setPosition();
std::string getName();
void getPin();
void print();
};
#endif
The source file is as follow:
#include <iostream>
#include "Customer.h"
Customer::Customer(){}
Customer::Customer(std::string name, std::string address, float lat, float lon){
this->name = name;
this->address = address;
this->position = GeoPosition(lon, lat);
}
std::string Customer::getName(){
return this->name;
}
void Customer::getPosition(){}
void Customer::setPosition(){}
void Customer::getPin(){}
void Customer::print(){}
int main(){
return 0;
}
My question is that every time I create a new object, I want to increment the member variable Customer::anzahl
.
I know that it is not possible to initialize a static member in the constructor like this:
Customer::Customer(std::string name, std::string address, float lat, float lon){
this->name = name;
this->address = address;
this->position = GeoPosition(lon, lat);
anzahl++;
}
Is there a workaround or what can I do to increment anzahl
every time I create a new object of Customer?
I get always the following error:
Undefined symbols for architecture x86_64:
"Customer::anzahl", referenced from:
Customer::Customer(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, float, float) in Customer-141888.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I can't initialize or reinitialize an static member in the constructor of the source file. What should I do instead?