I have been having trouble with calling upon my class/constructor in C++. And I can't exactly figure out why, [my game title.cpp]
#include <iostream>
#include <vector>
#include <cstdlib>
#include "Header1.h"
#include <string>
using namespace std;
int main() {
string playerName;
vector<string> options = { "Frodo", "Bilbo", "Wongle", "Aragorn", "-TYPE YOUR NAME-" };
cout << "Welcome, my friend!" << endl;
for (int i = 0; i < options.size(); ++i) {
cout << static_cast<char>('A' + i) << " - " << options[i] << endl;
}
char playerAnswer;
cin >> playerAnswer;
int choiceIndex = toupper(playerAnswer) - 'A';
if (choiceIndex <= 3 && choiceIndex < options.size() - 1) {
playerName = options[choiceIndex];
}
else {
cout << "You haven't picked any of the default options. What name would you like instead?" << endl;
cout << "Enter your custom name below:" << endl << "> ";
cin >> playerName;
}
cout << "Welcome to your new world, or your death. We hope you have fun, " << playerName << "." << endl;
cout << "In this world, you can decide whether you shall let it prosper, be its hero, or end its suffering. What will you choose?" << endl;
cout << "TYPE ANY LETTER TO CONTINUE" << endl << "> ";
char CONTINUE;
cin >> CONTINUE;
cout << "\nYou wake up on the soft padded grass. When you take a look around, all you see are layers of trees surrounding you." << endl;
cout << "As soon as you take a closer look around, you find an opening between the trees. What will you do?" << endl;
cout << "1. Look around more in your area\n2. Examine the opening\n3. Walk through the opening\n> ";
int decisionOpening1 = 0;
cin >> decisionOpening1;
switch (decisionOpening1) {
case 1: {
cout << "You soon find a shrine right behind you, covered in moss. You feel some type of gravitational pull towards it. What shall you do?" << endl;
cout << "1. Attempt to destroy the shrine\n2. Examine the shrine\n3. Examine the opening in the tree line\n4. Walk through the opening of the tree line\n> ";
int decisionOpening1C1 = 0;
cin >> decisionOpening1C1;
switch (decisionOpening1C1) {
case 1:
cout << "\nYou managed to muster all the energy you have to destroy this shrine with a single punch. Upon landing a blow to the shrine, your punch immediately got reflected back towards your face." << endl;
cout << "Causing your whole body to go into cardiac arrest from your amazing punch getting redirected to you. Not soon after, you then died due to a lack of oxygen going to that tiny little brain of yours." << endl;
cout << "\nENDING 1 OF 927\nWould you like to try again, " << playerName << "? ";
char ending1;
cin >> ending1;
break;
case 2: {
D20 myDice;
break;
}
default:
cout << "INVALID INPUT";
}
}
break;
case 2:
cout << "You soon come to the conclusion that, once you've taken a closer look at the opening, there was a malnourished elf by the exit awaiting for someone to appear." << endl;
break;
case 3:
// other code
break;
default:
cout << "INVALID INPUT";
}
return 0;
}
and here is my other files too [header1.h]
#ifndef HEADER_H
#define HEADER_H
#include <string>
class D20 {
public:
D20();
void setRoller(const std::string& newRoller);
std::string getRoller() const;
private:
std::string roller;
};
#endif
[my other source code for the constructor]
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
class D20 {
public:
D20() {
setRoller("DefaultRoller");
}
void setRoller(string newRoller) {
roller = newRoller;
}
string getRoller() const {
return roller;
}
private:
string roller;
};
int main() {
D20 myDice;
srand(static_cast<unsigned>(time(nullptr)));
for (int i = 1; i <= 1; i++) {
int random = 1 + (rand() % 20);
cout << random << endl;
}
myDice.setRoller("NewRoller");
string rollerValue = myDice.getRoller();
cout << "Roller: " << rollerValue << endl;
return 0;
}
Can you please help me out?
I've tried using chatGPT to help and no use was working sadly, or I might be doing something wrong when I'm asking for help from it.
Change
int main()toint dice()When I changed the second
int main()toint dice(), the link error went away.I was able to compile, and run a little bit, although I am not sure everything was as you wanted. Part of the problem is that I am unsure what you want the program to do.
I won't be able to solve all your problems, but I think I can help with
class D20that you asked about in your question.File structure for
class D20In many programs, a class like
D20is broken into two files:D20.hcontains the class declaration, andD20.cppcontains the definitions for the member functions declared inD20.h.The header file
D20.his the same as what you calledheader1.h. Changing the file name and include guards gives the following:The
.cppfile uses the scope resolution operator::to reference the function names from classD20:D20::D20is the constructorD20::setRolleris the setterD20::getRolleris the getterNote, as well, that the
.cppfile includes the headerD20.h. This gives it access to the class declaration.The definition you gave for function
setRollerhad a different function signature from the one used in header fileD20.h. In the code below, I changed them to match.With these two files in place, you can erase
class D20from what you called "[my other source code for the constructor]". Afterwards, be sure to add#include "D20.hin its place.In addition, you can include the header
D20.hin any file that needs to useclass D20. One of those is the file you calledmy game title.cpp.