-1

I'm teaching myself how to use classes in C++, and I run into a issue when I do:

cout << One.Player::inventory(slotOne);

I'm not sure why, but it tells me:

error "slotOne" was not declared in this scope.

When I compile. I'm using Code::Blocks, and using the GNU/GCC compiler on Windows 10 version 1709, build 12699.192, and have never had this sort of issue before. What have I done wrong, and how can I fix it?

#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <algorithm>

using namespace std;

std::string longsword = "Longsword: 1d8 slashing";
std::string shortsword = "Shortsword: 1d6 slashing";
std::string dagger = "Dagger: 1d4 slashing";
std::string falchion = "Falchion: 1d10 slashing";
std::string longbow = "Longbow: 1d8 piercing \t Range: 110/330 yards";

class Player {
public:
    std::string inventory();
    int health();
    int hunger();
    int exhaustion();
};

int main() {
    srand(time(NULL));
    string playername;
    Player One;
    cout << "Welcome to -WIP-." << endl;
    cout << "What is your name? \n";
    cin >> playername;
    cout << One.Player::inventory(slotOne);
    return 0;
}
int Player::health()
{
    int hp = 10;
    cout << "Current health is: " << hp << endl;
    return 0;
}

std::string Player::inventory() {
    std::string slotOne = longsword;
    std::string slotTwo = longbow;
    return 0;
}
Finnegan
  • 33
  • 5
  • 1
    First thing, teach yourself how to format code in order to make it readable. –  Feb 06 '18 at 00:18
  • 1
    The only instance of a `slotOne` variable I see is the one that exists inside Player::inventory(). This variable also doesn't do anything since it's destroyed before ever being read. – MrEricSir Feb 06 '18 at 00:20
  • @NeilButterworth Thanks. That's totally what I needed. For the record, the IDE has it nicely formatted. – Finnegan Feb 06 '18 at 00:27
  • @Ron Thank you! Wait. Why should I lose the `using namespace std;`? – Finnegan Feb 06 '18 at 00:27
  • You need to `#include ` –  Feb 06 '18 at 00:35
  • Unrelated: [Link to a better way to do dice:.](https://stackoverflow.com/a/31061880/4581301) – user4581301 Feb 06 '18 at 00:46
  • Unrelated (and I wouldn't bother if it hadn't come up): [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Feb 06 '18 at 00:47
  • Missing ``, `hunger()`, and `exhaustion()`. – Jive Dadson Feb 06 '18 at 00:57
  • 1
    Your code has undefined behavior. Returning a null pointer `0` to construct a `std::string` is undefined and will most likely crash – Guillaume Racicot Feb 06 '18 at 01:10
  • 1
    You really ought to avoid `using namespace std` - it is a bad habit to get into, and [can silently change the meaning of your program](/q/1452721) when you're not expecting it. Get used to using the namespace prefix (`std` is intentionally very short), or importing *just the names you need* into the *smallest reasonable scope*. – Toby Speight Feb 06 '18 at 13:17
  • `hunger()` and `exhaustion()` are in there, along with ``. I'm not sure why you don't see them, @JiveDadson – Finnegan Feb 06 '18 at 16:48

1 Answers1

2

From the code I see the scope of slotOne variable is within Player::inventory() function and you are trying to access the variable slotOne within main function where in you don't have a variable by the name slotOne declared within its scope.

Again your code has multiple errors. The inventory function's written type is string but you are trying to return an int value instead your inventory function should either return int like below

int Player::inventory()
{
    std::string slotOne = longsword;
    std::string slotTwo = longbow;
    return 0;
}

or you should convert your return value to string if you want to return a string value

Maddy
  • 774
  • 5
  • 14