Why can't I declare string inside a public class?

#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <string>

using namespace std;

class Player
{
public:
string player1; //Where is the mistake,variable is not accepted.
};





#endif
Last edited on
There's a ; missing from the end of the class.
salem c thank you, but I'm not getting the fact that why inside public, string isn't being detected or it is not taking string variables, but outside public i can declare strings.
What can we say except that it isn't true.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std; // shouldn't use this in header files

class Player
{
public:
    string player1;
};

int main()
{
    Player p;
    p.player1 = "bob";
    cout << p.player1 << '\n';
}

Thank you
@Validity Well, now I'm curious. What was the problem?
@Mikeyboy, if I use #ifndef PLAYER_H,
visual code doesn't accept string variables inside public.
Last edited on
> #ifndef PLAYER_H
You do know that these have to be unique across all the header files in your project.

If you use PLAYER_H as a guard for some other file as well, then it will all go horribly wrong.

@Validity I assume you understand why multiple-inclusion guards are important, and shouldn't be deleted?

Clearly, you're doing something wrong with them. Salem C may well be correct that you're accidentally using the same guard in more than one header.
@MikeyBoy I fixed the problem, I was using the same header in another saved file which I forgot to remove
Topic archived. No new replies allowed.