-3

I've looked at a lot of solutions for this problem but none have worked

Main:

#include "player.cpp"
#include "player.h"

#include <iostream>
#include <SDL.h>

using namespace std;

int main() {
     player p;
     SDL_Init(SDL_INIT_EVERYTHING);
     for (;;) {
          p.move()
     }
}

player.h:

#pragma once

#ifndef PLAYER_H
#define PLAYER_H

class player {
    private:
        short x = 0;
        short y = 0;
    public:
        void move() {
            std::cout << x << '\n';
            x += 1;
        }
};

#endif

player.cpp:

#include "player.h"

#include <iostream>

It keeps saying cout is not a member of std, what am I doing wrong?

Jowy47
  • 13
  • 2
  • 3
    Never ever do that: `#include "player.cpp"` That's wrong! – πάντα ῥεῖ Jun 09 '21 at 20:00
  • 3
    You need to `#include ` in player.h if you are going to put calls to std::cout in the header. – drescherjm Jun 09 '21 at 20:00
  • 1
    Not what you asked, but if you're on Windows, `int main()` might blow up in your face. SDL normally expects `int main(int, char **)`. – HolyBlackCat Jun 09 '21 at 20:01
  • Side note: If the cpp file doesn't implement anything, leave it out. Save you a few seconds when compiling. – user4581301 Jun 09 '21 at 20:05
  • @user4581301, I disagree. It ensures the header compiles with no prior includes before other code starts using it. (You could also make an argument to move that to the testing section of the project.) – chris Jun 09 '21 at 20:07

1 Answers1

4

The player.h file does not have a #include <iostream> in it.

You have two choices:

  1. Make every header file able to stand on its own.

  2. Ensure that you document and meet the pre-requisites for every header file you include.

You have done neither of these things.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Also, OP probably doesn't want to be defining `move` in `player.h` (just adding the include will result in duplicate definitions at link time) – Silvio Mayolo Jun 09 '21 at 20:01
  • 2
    My rule of thumb is to strongly prefer option 1. People usually don't read documentation until it's too late. – user4581301 Jun 09 '21 at 20:04