not a member of std

This problem has caused me hours of getting nothing done over the last 2 days...
If i make my own class and include a library in the .cpp, I keep getting ONE of these errors:
'vector' not a member of std;
'string' not a member of std;

in my .cpp file i have #include <vector> and #include <string>
i do NOT use "using namespace std;"

std::string x; is now working, when it would not work for my earlier class.
and yet
std::vector<int> my_vector; gives me the vector error.

I can't figure out where it is going wrong. Sometimes it throws one error, and at times it throws the other. The code for the 2 classes is identical.
This is all very confusing... so here is the code that is throwing the vector error and NOT the string error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef PLAYER_H
#define PLAYER_H


class Player
{
    public:
        Player();
        ~Player();
    private:
        std::string name;
        std::vector<Card> hand1;

};

1
2
3
4
5
6
7
8
9
10
11

#include "Player.h"
#include "Card.h"
#include <string>
#include <vector>
#include <iostream>


Player::Player(){}

Player::~Player(){}



Last edited on
Include <string> and <vector> inside the header file.
1
2
3
4
5
6
7
8
9
#ifndef PLAYER_H
#define PLAYER_H

// Add these
#include <string>
#include <vector>

class Player
{

std::mystring x; is now working, when it would not work for my earlier class.
You added your own class to the std namespace? Don't do that.
I'm sorry that was a typo!! I should have just copy and pasted my code.

std::string x;

Thank you for the help. I'm going to try it out now!

--------

Wow... I thought you only had to include it in the .cpp!
I need to learn more about what happens when you make a class!

Thank you guys for saving me hours of time!
Last edited on
I need to learn more about what happens when you make a class!

You need to learn more about what happens when you include a file. #include is just an automatic copy paste. It simply inserts the content of the file as-is. Instead of including a file you can copy the content of the file and paste it where you would have placed the include. That would give the exact same result.

Sometimes it can look like you don't need to include some headers in a header file because they are included by the source file. If you have no includes inside Player.h you could still make it work by including the headers that Player.h needs before including Player.h.
1
2
3
#include <string>
#include <vector>
#include "Player.h" 

But I don't recommend doing it this way. It's much better if you always try to make sure that the header files can be compiled on their own without relying on the file that includes them to work.
Topic archived. No new replies allowed.