在c++中添加第二个.h/cpp文件时遇到麻烦
trouble with adding second .h/cpp file to C++
我对c++相当陌生,看了看其他几个关于如何包含第二个cpp文件的主题,不确定我到底做错了什么…我主要从网格数组和枚举中得到错误,显然我不能为minimax.h文件使用void ?main.cpp文件的其余部分工作良好,只要我单独编译它
minimax.cpp
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
void minimax(Grid& grid, Color color_player)
{
int AI_Play;
/* initialize random seed: */
srand (time(NULL));
/* generate secret number between 0 and grid size: */
AI_Play = rand() % grid.size() + 0;
play(grid, AI_Play, color_player)
}
minimax.h
#ifndef MINIMAX_H_INCLUDED
#define MINIMAX_H_INCLUDED
minimax(Grid& grid, Color color_player)
#endif // MINIMAX_H_INCLUDED
main.cpp
#include <SDL.h>
#include <stdio.h>
#include <array>
#include <iostream>
#include <minimax.h>
using namespace std;
//Connect Four Array
#define COLUMN 6
#define ROW 7
//Screen dimension constants
const int SCREEN_WIDTH = 364;
const int SCREEN_HEIGHT = 312;
enum Color {red, black, nothing};
typedef array<array<Color, ROW>, COLUMN> Grid;
您需要将Grid
和Color
的声明移动到minimax.h -否则它们将无法在minimax.cpp中看到。您还需要包含minimax.cpp
同样,你应该在你的函数中声明返回类型'void'。文件。
尝试更改
#include <minimax.h>
#include "minimax.h"
<header name>
为内置标头。
您需要在minimax.h
中包含Color
和Grid
声明,或者将其放在文件的某个地方,然后将其包含在minimax.h
中。你的minimax.cpp
也应该包括minimax.h
minimax()
不知道Grid
和Color
是什么,因为您没有在定义minimax()
之前定义它们。您的代码应该看起来更像这样:
minimax.h
#ifndef MINIMAX_H_INCLUDED
#define MINIMAX_H_INCLUDED
#include <array>
//Connect Four Array
#define COLUMN 6
#define ROW 7
enum Color {red, black, nothing};
typedef std::array<std::array<Color, ROW>, COLUMN> Grid;
void minimax(Grid& grid, Color color_player);
#endif // MINIMAX_H_INCLUDED
minimax.cpp
#include "minimax.h"
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
void minimax(Grid& grid, Color color_player)
{
int AI_Play;
/* initialize random seed: */
srand (time(NULL));
/* generate secret number between 0 and grid size: */
AI_Play = rand() % grid.size() + 0;
play(grid, AI_Play, color_player);
}
main.cpp
#include <SDL.h>
#include <stdio.h>
#include <iostream>
#include "minimax.h"
//Screen dimension constants
extern const int SCREEN_WIDTH;
extern const int SCREEN_HEIGHT;
// use minimax() as needed, eg...
int main()
{
Grid grid;
...
minimax(grid, black);
...
return 0;
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 命名空间中具有.h和.cpp文件的类
- 为什么我的主文件.cpp不打印头文件中的任何内容
- 生成文件错误 - 找不到文件 - *.cpp
- 如何在文件.cpp gtkmm中声明小部件
- 没有实现文件(.cpp)的派生类
- 如何使用"CMakeLists.txt"中的add_library将整个文件(.cpp,.h等)包含在目录中
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 关于 Linux .so 文件无法链接到主文件.cpp文件
- 文件.cpp从Windows到Linux
- Ubuntu C++ 编译器错误: g++: 错误: 文件.cpp: 没有这样的文件或目录
- 如何将文件.cpp编译为本机编译
- 使用模板类时,似乎无法包含除 main 以外的任何 cpp 文件.cpp
- 无法编译C++文件.cpp。C++98模式
- 为什么C++头文件不需要包含实现文件 (.cpp)
- 如何访问位于独立文件(.cpp)中的非成员函数
- 如何在Visual Studio Code中通过键盘快捷键切换头文件/cpp文件
- 如何将数据从stringstream写入文件(CPP)
- 为什么filestream不把空白写入文件cpp ?