我如何使用这些 类?

How do I work with these classes?

我是 C++ 的新手,我正在尝试开始一个项目,每次我创建 ATM 的新实例时 class 它都会将 accountID 增加 1 并显示当前帐户 ID。 这是我的代码:

// Bank ATM.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "ATM.h"


int main()
{
    ATM abunch[15];
    for (int i = 0; i < 15; i++){
        abunch[i] = ATM();
    }
    return 0;
}


//ATM.h
 #include "stdafx.h"
 #ifndef atm
#define atm
class ATM {
    static int accountID;

public:
    ATM();
};
int ATM::accountID = 0;
#endif


//ATM.cpp
#include "stdafx.h"
#include "ATM.h"
#include <iostream>
ATM::ATM() {
    ++accountID;
    std::cout << accountID;
}

我收到以下错误消息:

我做错了什么?

因为 ATM::accountID.h 文件中声明,在 class 之外,每次该文件包含在另一个文件中时都会全局声明。你包括它两次;在 main.cppATM.cpp 中。这是一个禁忌。

声明需要移动到ATM.cpp

//ATM.h
 #include "stdafx.h"
 #ifndef atm
#define atm
class ATM {
    static int accountID;

public:
    ATM();
};
int ATM::accountID = 0;   // <--- remove this line
#endif


//ATM.cpp
#include "stdafx.h"
#include "ATM.h"
#include <iostream>
int ATM::accountID = 0;   // <----put it here
ATM::ATM() {
    ++accountID;
    std::cout << accountID;
}