Was kann man gegen den “Undefined Reference to”-Error machen?
Ich habe angefangen C++ und Unreal Engine zu lernen also habe ich um C++ besser zu verstehen versucht ein Konsolen Spiel dazu zu schreiben, aber wenn ich mein Spiel ausführe schließt das Terminal Fenster (, welches erscheint wenn man eine Consolen-App mit VS ausführt) direkt und in Online Debuggern erscheint nur “Undefined Reference to” als Fehler und danach stehen Sachen die ich im Code mal hatte aber nicht mehr enthalten sind.
Hier ist der Code von meinem Spiel (auch wenn mein Code hässlich ist):
#include <iostream>
#include <string>
using namespace std;
class Var {
public:
static int roundsUntilTheEnemyComes;
static int player_power;
static int enemy_power;
static int rounds;
static bool bIsDead;
static int Money;
static int towers, soldiers, schools, soldierSchools, archerSchools;
};
class Voids {
public:
void Enemy() {
Var Var;
if (Var.rounds == 0) {
Var.enemy_power += 50;
}
else if (Var.rounds <= 5 && Var.rounds != 0) {
Var.enemy_power += 90;
}
else if (Var.rounds <= 15 && Var.rounds >= 5) {
Var.enemy_power += 150;
}
else if (Var.rounds <= 50 && Var.rounds >= 15) {
Var.enemy_power *= 2;
}
else if (Var.rounds <= 75 && Var.rounds >= 50) {
Var.enemy_power *= 5;
}
else if (Var.rounds > 75) {
Var.enemy_power *= 10;
} if (Var.player_power > Var.enemy_power) {
Var.bIsDead = false;
}
else {
Var.bIsDead = true;
}
} void IsDeadCheck() {
Var Var;
if (Var.bIsDead) {
cout << "\n \n \n!!!You are dead!!!";
}
} void MainGame() {
int dec;
cin >> dec;
if (dec == 1) {
cout << "HI";
}
}
};
int main() {
cout << "!!!Game Start!!! \n";
Var Var;
Voids GameVoids;
Var.Money = 0;
Var.bIsDead = false;
Var.rounds = 0;
Var.enemy_power = 0;
Var.player_power = 0;
Var.roundsUntilTheEnemyComes = 3;
while (true) {
if (Var.roundsUntilTheEnemyComes != 0) {
cout << "What do you want to buy? \n";
cout << "(1) A Tower | (2) An Archer | (3) An Archer School | (4) A Soldier | (5) A Soldier School | (6) A Wall | (7) A Goldmine | (8) A School";
GameVoids.MainGame();
if (Var.roundsUntilTheEnemyComes == 1) {
cout << "The Power of the Enemy is " + to_string(Var.bIsDead);
}
Var.roundsUntilTheEnemyComes--;
}
Var.roundsUntilTheEnemyComes = 0;
GameVoids.Enemy();
GameVoids.IsDeadCheck();
if (Var.bIsDead) {
break;
}
}
return 0;
}
Var Var is not at all.
You can’t define a class and then create an object of the class, that’s the same. The compiler can’t resolve this.
The static variable also exist without an object of the class.
In addition, the syntax for static variables is different, according to my memory. And… if that’s all static, why don’t you just take global variables or A static object?
If I don’t mind, you have to initialize the statics.
Why don’t you read this?
https://en.cppreference.com/w/cpp/language/static