【问题标题】:Separate Class File errors? [closed]单独的类文件错误? [关闭]
【发布时间】:2014-10-22 18:15:45
【问题描述】:

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

立方体.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 已停止工作”?

【问题讨论】:

  • 始终将#include "stdafx.h" 设为第一个reference
  • 你的错误是什么?
  • 据我所见,事情应该可以正常工作,但是您没有发布整个代码,因此错误可能是其中的一部分。一个可能的小错误是您的#include "cube.h" 上的大写与主要的实际文件名Cube.h 不同,某些操作系统不喜欢它,因此实际上不会链接到该文件,任何尝试都会出错引用立方体。
  • 对不起,我的大部分错误都是“未声明的标识符”错误和“必须有一个类/结构/联合”

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


【解决方案1】:

这是 Visual Studio 特有的(预编译头文件):

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

你的代码变成:

立方体.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
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 2016-02-16
    • 2013-03-08
    • 1970-01-01
    • 2018-04-15
    • 2012-08-23
    • 1970-01-01
    相关资源
    最近更新 更多