2
我對C++有點新,所以我正在製作一個文本RPG類的東西來測試我學到的東西。我想這樣做是爲了讓玩家進入他們三個角色中的每一個角色的等級(法師,戰士,弓箭手等)。對整數靜態數組的未定義引用
字符類存儲在一個名爲cls []的int整數靜態數組中。我寧願讓事物保持靜態,也不要讓這個類成爲對象,因爲幾乎遊戲中的所有東西都會嘗試訪問類的成員。但由於某種原因,它一直給我提供錯誤信息:未定義的引用`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'
}
是的,他可以,只要'setClass()'被聲明爲靜態方法。問題是你需要在你的一個源文件中定義int playerVars :: cls [3]。嘗試將'int playerVars :: cls [3] = {0,0,0};'放在'#include「playerVars.h」' – cxyzs7
之後''可能重複[什麼是未定義的引用/未解析的外部符號錯誤,以及如何我解決它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-doi-i-fix) –