c ++在单独的.cpp和.h文件中创建一个类

时间:2016-07-28 22:50:23

标签: c++ visual-studio-2010

我正在尝试宣布课程" graphics"但在graphics.cpp中,我收到错误。

  

'图形'不是类或命名空间名称。

它说错误的位置是

  

图形::图形()

我正在使用Visual Studio 2010,在我的代码中,图形被突出显示为一个类..但它显然不被graphics.cpp视为一个类?有谁知道这里的问题是什么?

这是我的代码

//graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H

struct SDL_Window;
struct SDL_Renderer;

class graphics
{
public:
     graphics();
     ~graphics();
private:
     SDL_Window* _window;
     SDL_Renderer* _renderer;
};

#endif

然后

//graphics.cpp
#include "graphics.h"
#include "stdafx.h"

graphics::graphics() {}
graphics::~graphics() {}

1 个答案:

答案 0 :(得分:3)

如果您使用的是预编译的标题

#include "SDL.h"
#include "graphics.h"
#include "stdafx.h" <<<<< must always be included before anything else

更改为

#include "stdafx.h"
#include "SDL.h"
#include "graphics.h"

编译器应该输出此错误以及您给定的错误。