0

I'm new to C++ and I am trying to separate class files for a game I made, but when i do, VS generates a load of errors.

Cube.h:

#include "stdafx.h"
#ifndef CUBE_H
#define CUBE_H

struct PlayerCube
{
  //code

};

#endif //CUBE_H

Cube.cpp:

#include "cube.h"
#include "stdafx.h"

using namespace std;

PlayerCube::PlayerCube(){}

void PlayerCube::cube_movement(){}

void PlayerCube::show_cube(){}

Main:

#include "cube.h"
#include "stdafx.h"

using namespace std;

int main ()
{
    //code
}

any ideas would help! :)

EDIT: Keats answer reduced my errors from 96 down to 3!

I now just have 3 C2679 errors stating that "binary >> : no operator found"

EDIT: Found out my problems, just one more remains!

Everything builds fine, but when I run my program, it crashes, ".exe has stopped working"?

Tom
  • 2,372
  • 4
  • 25
  • 45
  • 2
    Always make `#include "stdafx.h"` the *first* include. [reference](http://stackoverflow.com/questions/16040689/why-stdfax-h-should-be-the-first-include-on-mfc-applications) – crashmstr Oct 22 '14 at 18:17
  • 4
    What are your errors? – Lochemage Oct 22 '14 at 18:19
  • 1
    From what I see, things should be working fine, but you didn't post the entire code up so the errors might be part of what's in there. One small possible error is the capitalization on your `#include "cube.h"` from main differs from the actual file name `Cube.h`, some OS's do not like it and therefore won't actually link to the file, you'll get errors on anything that tries to reference cube. – Lochemage Oct 22 '14 at 18:26
  • sorry, most of my errors are "undeclared identifer" errors and "must have a class/struct/union" – Tom Oct 22 '14 at 18:36

1 Answers1

1

This is specific to Visual Studio (precompiled headers) :

  • Remove the inclusion of stdafx.h from cube.h
  • Always include stdafx.h first in cpp files

Your code becomes :

Cube.h:

#ifndef CUBE_H
#define CUBE_H

struct PlayerCube
{
  //code

};

#endif //CUBE_H

Cube.cpp:

#include "stdafx.h"
#include "cube.h"

using namespace std;

PlayerCube::PlayerCube(){}

void PlayerCube::cube_movement(){}

void PlayerCube::show_cube(){}

Main:

#include "stdafx.h"
#include "cube.h"

using namespace std;

int main ()
{
    //code
}

If you still have errors, please include them in your question.

KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83