单独的类文件错误?

时间:2014-10-22 18:15:46

标签: c++ visual-studio precompiled-headers

我是C ++的新手,我正在尝试为我制作的游戏分离类文件,但是当我这样做时,VS会产生大量错误。

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(){}

主:

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

using namespace std;

int main ()
{
    //code
}

任何想法都会有所帮助! :)

编辑: 济慈回答将我的错误从96减少到3!

我现在只有3个C2679错误,说明“二进制>>:找不到运算符”

编辑: 发现我的问题,还有一个遗骸!

一切都很好,但是当我运行我的程序时,它会崩溃,“。exe已停止工作”?

1 个答案:

答案 0 :(得分:1)

这是特定于Visual Studio(预编译头文件):

  • 从cube.h中删除stdafx.h
  • 始终首先在cpp文件中包含stdafx.h

您的代码变为:

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(){}

主:

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

using namespace std;

int main ()
{
    //code
}

如果您仍有错误,请将其包含在您的问题中。

相关问题