I'm learning a new Language C++ with knowledge used from Java not JavaScript
.
I'm trying to make a Parent class called Command
with a derived class CmdHelp
.
In java I would make an abstract class
called Command.java
, and make the class CmdHelp.java
extend to Command
, and in the constructor of CmdHelp.java
I would have a static ArrayList<Command>
inside of Command.java
that will add it's self like this.
public CmdHelp() {
// Set protected variables here like label, usage, and description for the CmdHelp class.
cmds.add(this);
}
This is Java, and I'm trying to mirror this into C++. When I created a static vector<class Command>
variable in the Command.h
header file, and then in the CmdHelp.cpp
constructor, and did
CmdHelp::CmdHelp() {
cmds.push_back((Command) *this);
}
It doesn't seem to add the instance to the vector list. Bellow is all my C++ code, and files.
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "Command.h"
using namespace std;
int main() {
cout << "Initiated Command Line:" << endl;
while (true) {
cout << "> ";
string input = "";
cin >> input;
bool found = Command::checkCommand(input);
if (found) {
Command::getCommand(input).run();
}
else {
Command::printNotFound();
}
}
return 0;
}
Command.h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Command {
public:
Command();
// VARIABLES //
string label;
string usage;
string description;
// STATIC VARIABLES //
static vector<class Command> cmds;
// SETTER FUNCTIONS //
void setLabel(string);
void setUsage(string);
void setDescription(string);
// GETTER FUNCTIONS //
string getLabel();
string getUsage();
string getDescription();
// INHERIT FUNCTIONS //
virtual void run(); // trying to make this function an "abstract" function like I would in Java.
// STATIC FUNCTIONS //
static Command getCommand(string);
static bool checkCommand(string);
static void printNotFound();
};
Command.cpp
#include "Command.h"
#include <iostream>
#include <string>
using namespace std;
Command::Command() {
}
// SETTER FUNCTIONS //
void Command::setLabel(string label) {
Command::label = label;
}
void Command::setUsage(string usage) {
Command::usage = usage;
}
void Command::setDescription(string description) {
Command::description = description;
}
// GETTER FUNCTIONS //
string Command::getLabel() {
return label;
}
string Command::getUsage() {
return usage;
}
string Command::getDescription() {
return description;
}
void Command::run() {}
// STATIC FUNCTIONS //
Command Command::getCommand(string label) {
for (int i = 0; i < cmds.size(); i++) {
if (cmds[i].getLabel() == label) {
return cmds[i];
}
}
return Command();
}
bool Command::checkCommand(string label) {
for (int i = 0; i < cmds.size(); i++) {
if (cmds[i].getLabel() == label) {
return true;
}
}
return false;
}
void Command::printNotFound() {
cout << "Command not found! Type \"help\"!" << endl;
}
CmdHelp.h
#include "Command.h"
class CmdHelp: public Command {
public:
CmdHelp();
void run();
};
CmdHelp.cpp
#include "CmdHelp.h"
#include <iostream>
#include <string>
using namespace std;
CmdHelp::CmdHelp() {
setLabel("HELP");
setUsage("/help");
setDescription("Shows the help screen!");
cmds.push_back((Command) *this);
}
void CmdHelp::run() {
cout << cmds[0].getLabel() << endl;
}
Please help me it gives these weird errors. And sorry if this is SUPER simple, I can't find a solution because I don't know what this is even called? This is only my first week of C++ when I know Java for 5 years. Please do Java references so I can understand easier.
THANK YOU.
Here are my errors:
1>CmdHelp.obj : error LNK2001: unresolved external symbol "public: static class std::vector > Command::cmds" (?cmds@Command@@2V?$vector@PEAVCommand@@V?$allocator@PEAVCommand@@@std@@@std@@A)
1>Command.obj : error LNK2001: unresolved external symbol "public: static class std::vector > Command::cmds" (?cmds@Command@@2V?$vector@PEAVCommand@@V?$allocator@PEAVCommand@@@std@@@std@@A)
1>C:\Users\Dennis\Documents\Projects\CPPTutorial\CPPTutorial\x64\Debug\CPPTutorial.exe : fatal error LNK1120: 1 unresolved externals