0

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?

Mortey
  • 1
  • 1
  • As @NathanOliver said, how does it not work? Share what you see not working – Griffin May 08 '17 at 12:45
  • I get always the following error: – Mortey May 08 '17 at 13:00
  • Undefined symbols for architecture x86_64: "Customer::anzahl", referenced from: Customer::Customer(std::__1::basic_string, std::__1::allocator >, std::__1::basic_string, std::__1::allocator >, 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) – Mortey May 08 '17 at 13:00
  • 1
    Specify it in your source file right below your includes like this `int Customer::anzahl = 0;` – Griffin May 08 '17 at 14:14

0 Answers0