对静态整数数组的未定义引用

undefined reference to a static array of integers

本文关键字:未定义 引用 数组 静态 整数      更新时间:2023-10-16

我对c++有点陌生,所以我正在制作一个文本RPG之类的东西来测试我所学到的东西。我想让玩家进入他们的三个角色的职业(法师,战士,弓箭手等)。

字符类存储在名为cls[]的静态整型数组中。我更喜欢保持静态,而不是创建这个类的对象,因为几乎游戏中的所有内容都会尝试访问这个类的成员。但出于某种原因,它一直给我的错误信息:未定义的引用' playerVars::cls'。我猜这意味着它找不到数组之类的?我非常感谢你能就这个问题提供任何线索。

intro.h
-----------------------------
#ifndef INTRO_H
#define INTRO_H
#include <iostream>
using namespace std;
class intro
{
    public:
        intro();
        int inint;
        void classDecide(int charUsed);
};
#endif

intro.cpp
-----------------------------
#include "intro.h"
#include "playerVars.h"
intro::intro()
{
    classDecide(0); //Calls the classDecide function using your first of 3 characters
}
void intro::classDecide(int charUsed)
{
    cin >> inint;   //Asks for the number of the class that you want
    playerVars::setClass(charUsed,inint);
}

playerVars.h
-----------------------------
#ifndef PLAYERVARS_H
#define PLAYERVARS_H
using namespace std;
class playerVars
{
    public:
        playerVars();
        static int cls[3];
        static void setClass(int classToSet, int setTo);
};
#endif

playerVars.cpp
-----------------------------
#include "playerVars.h"
playerVars::playerVars()
{
}
void playerVars::setClass(int classToSet, int setTo)
{
    cls[classToSet]=setTo;  //sets the class of player classToSet to setTo
            //undefined reference to `playerVars::cls'
}

添加

int playerVars::cls[3] = {0};
对playerVars.cpp