2011-12-12 63 views
2

当我尝试声明我的类“游戏”的实例时,收到编译错误“error:'游戏'没有为main.cpp命名类型”。错误:'x'未命名类型

如果可能无关紧要,但我使用的是代码块。从Game.cpp

相关代码Main.cpp的

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

class Game 
{ 
    private: 

    public: 
}; 

相关代码

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

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

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

我才刚刚开始学习C++,所以我可能忽略了一些东西明显:(

+0

在你Main.cpp的头文件,不包括类“游戏”的定义,所以你应该像Game.h一个文件中定义类游戏,和在你的Main.cpp中添加#include“Game.h” –

回答

3

在标题中包含“游戏”声明

notepad main.h =>

#ifndef MAIN_H 
#define MAIN_H 

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

记事本的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)定义你的类(在头的任何类型定义,常量等)沿

2)#include需要这些定义的任何.cpp文件中的头文件

3)编译时使用“-I”指定目录ectory(或目录)包含你的头

“希望帮助

0

也许你可以删除游戏类声明中Game.cpp并尝试创建一个名为‘Game.h’下../inclue/另一个文件如:

#ifndef _GAME_H_ 
#define _GAME_H_ 

class Game 
{ 
    private: 
    public: 
}; 

#endif 

并将此Header文件包含在Main.cpp中。然后我认为错误不会发生:)

因为我们通常使用.cpp文件作为类定义,.h文件作为声明,所以在Main.cpp中包含.h文件。

0

C文件或cpp文件是发生多重错误发生的问题。

每个

# pragma once 
Or 
# Ifndef __SOMETHING__ 
# define __SOMETHING__ 

Add the code ... 

# Endif