1

I want to count the ammount of objects which have been created by using a certain constructor. I have done this in languages like C# before so I created a static int variable which I'm incrementing in the constructor.

Before I show you the code - here is the compilement error:

Severity Code Description Project File Line Suppression State Error LNK2001 unresolved external symbol "private: static int Bill::count_of_created_bills" (?count_of_created_bills@Bill@@0HA) Ausgabenverwaltung c:\Users\xy\documents\visual studio 2017\Projects\Ausgabenverwaltung\Ausgabenverwaltung\Ausgabenverwaltung.obj 1

Here is the code:

#pragma once
class Bill
{
private:
    static int count_of_created_bills;
    int id;               // Unique Identification
    double ammount;       // Ammount of bill    
    int month;            // Month of bill (January = 0, February = 1 ...)
    int type_of_spending; // Type of spending (Food = 0 ...) 
public:
    Bill(int a, int m, int t):ammount(a), month(m), type_of_spending(t)
    {
        count_of_created_bills++;
        id = count_of_created_bills;
    }
};

The compilement error occurrs if I'm including this line:

Bill b(1, 2, 3);

Matthias Herrmann
  • 2,650
  • 5
  • 32
  • 66

1 Answers1

1

you forgot to add the initialization:

Bill::Bill(int a, int m, int t):ammount(a), month(m), type_of_spending(t)
    {

        std::cout << "::Ros-App!" << Foo::count_of_created_bills << std::endl;
        Foo::count_of_created_bills++;
        id = count_of_created_bills;
    }
int Bill::count_of_created_bills = 0;
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97