在另一个.cpp文件中调用的编写函数
writing functions that are called in another .cpp file
我想确保我正确调用功能。另一个.cpp中的功能是makeDeck()
和shuffle()
。我也不知道如何制作相关的标头文件或何时有帮助。有人可以写或引导我浏览我的deck.cpp
和deck.h
文件的语法: main.cpp
#include <iostream>
#include "deck.h"
using namespace std;
int main() {
int size;
do {
cout << "Enter size of deck (5-50): " << endl;
cin >> size;
} while (size<5 || size>50);
int** deck[size] = makeDeck(size);
shuffle(deck);
int score = 0;
char guess = NULL;
for (int** i = **deck; *i != NULL; i++) {
cout << "Score: " + score << endl;
cout << "Current card: " + *i<< endl;
cout << "Will the next card be higher (high) or lower (low) than " + *i + "?" << endl;
cin << guess;
if (guess == "higher" || guess == "high") {
if (*(i + 1) > *i)
score++;
else
score--;
}
if (guess == "lower" || guess == "low") {
if (*(i + 1) < *i)
score++;
else
score--;
}
}
}
一般而言,当您上课时使用.h文件,但是如果您认为您可以在另一个项目中使用它们,或者您想拥有一个清洁器的MAIM,则可以将它们用于功能文件。您拥有正确的文件#include
D,但是对于实际的.h和.cpp文件,您将需要以下内容:
deck.h
#ifndef DECK_H
#define DECK_H
int** makedeck(const int&);
void shuffle(int**);
#endif //DECK_H
deck.cpp
#include "deck.h"
int** makedeck(const int& size)
{
//Do something
}
void shuffle(int** deck)
{
//Do something else
}
对于您在main()
中不需要的任何功能,您将在.h文件中执行声明,然后在.cpp中定义它。没有真正的区别在MAIN中单独声明的功能并将其分为多个文件,只需确保将.H文件包含在任何需要使用您在.H中声明的函数的文件中。
相关文章:
- 函数调用中参数的顺序重要吗
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 变量没有改变?通过向量的函数调用
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 模板函数调用
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 析构函数调用
- 成员函数调用和C++对象模型
- 使用共享指针的函数调用,其对象应为 const
- C++:编译时检查匹配的函数调用对?
- 函数调用C++中的参数太少
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 返回指向对象的指针的函数调用是否为 prvalue?
- C++ 如何重载 [] 运算符并进行函数调用
- 代码的效率. 转到和函数调用
- 是同一作用域的函数部分中的函数调用
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 类型擦除的std::function与虚拟函数调用的开销