错误:"x"未命名类型 [英] error: 'x' does not name a type

查看:51
本文介绍了错误:"x"未命名类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试声明类'Game'的实例时,收到main.cpp的编译错误错误:'Game'没有命名类型".

When I try to declare an instance of my class 'Game' I receive the compile error "error: 'Game' does not name a type" for main.cpp.

如果没关系,但我正在使用代码块.

If probably doesn't matter but i'm using codeblocks.

Game.cpp中的相关代码

Relevant code from Game.cpp

#include "../include/main.h"

class Game
{
    private:

    public:
};

Main.cpp中的相关代码

Relevant code from Main.cpp

#include "../include/main.h"

Game g; //this is the line it is referring to

int main(int argc, char* args[])
{
    return 0;
}

我只是开始学习c ++,所以我可能忽略了一些显而易见的东西:(

I'm only starting to learn c++ so i probably overlooked something obvious :(

推荐答案

在标题中包含游戏"的声明

Include the declaration for "Game" in a header

记事本main.h =>

notepad main.h =>

#ifndef MAIN_H
#define MAIN_H

class Game
{
    private:
      ...
    public:
      ...
};
#endif
// main.h

记事本main.cpp =>

notepad main.cpp =>

#include "main.h"

Game g; // We should be OK now :)

int 
main(int argc, char* args[])
{
    return 0;
}

gcc -g -Wall -pedantic -I../include -o main main.cpp

注意您如何:

1)在标头中定义您的类(以及所有类型定义,常量等)

1) Define your classes (along with any typedefs, constants, etc) in a header

2)#在所有需要这些定义的.cpp文件中包含标头

2) #include the header in any .cpp file that needs those definitions

3)用"-I"进行编译,以指定包含标题的目录(一个或多个)

3) Compile with "-I" to specify the directory (or directories) containing your headers

'希望有所帮助

这篇关于错误:"x"未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆