C++ 错误:';x';不命名类型
当我尝试声明类“Game”的实例时,我收到main.cpp的编译错误“error:“Game”未命名类型” 如果可能没关系,但我使用的是代码块 Game.cpp中的相关代码C++ 错误:';x';不命名类型,c++,class,types,codeblocks,C++,Class,Types,Codeblocks,当我尝试声明类“Game”的实例时,我收到main.cpp的编译错误“error:“Game”未命名类型” 如果可能没关系,但我使用的是代码块 Game.cpp中的相关代码 #include "../include/main.h" class Game { private: public: }; Main.cpp中的相关代码 #include "../include/main.h" Game g; //this is the line it is referring to
#include "../include/main.h"
class Game
{
private:
public:
};
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++,所以我可能忽略了一些明显的东西:()包含了一个标题中的“游戏”声明 记事本main.h=>
#ifndef MAIN_H
#define MAIN_H
class Game
{
private:
...
public:
...
};
#endif
// main.h
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) 在标题中定义类(以及任何typedef、常量等)
2) #在需要这些定义的任何.cpp文件中包含头
3) 使用“-I”编译以指定包含标题的目录
'希望这有帮助也许您可以删除Game.cpp中的游戏类声明,并尝试在../include/like下创建另一个名为“Game.h”的文件:
#ifndef _GAME_H_
#define _GAME_H_
class Game
{
private:
public:
};
#endif
并将此头文件包含在Main.cpp中。那么我认为错误不会发生:)
因为我们通常使用.cpp文件进行类定义,使用.h文件进行声明,然后在Main.cpp中包含.h文件。C文件或cpp文件是编译时发生多个错误的问题 每个文件的头文件
# pragma once
Or
# Ifndef __SOMETHING__
# define __SOMETHING__
Add the code ...
# Endif
在Main.cpp中,您没有包含类“Game”的定义,因此您应该在Game.h这样的文件中定义类游戏,并在Main.cpp中添加#include“Game.h”