C++ 从实现文件中的 main 调用函数
C++ Calling a function from main within an implementation file
假设我的"Main.cpp"文件中有一个函数,我需要在类中的实现.cpp文件中运行。我该怎么做?
假设我有 Main.cpp它有函数 findDate,它需要在我类中名为 Date 的函数中调用。包含 Main.cpp 文件的问题在于所有内容都在重新初始化,我似乎无法让 #ifndef 在 Main.cpp 文件中工作。谢谢!
您应该在文件 main.h 中声明(但不是定义)findDate。然后在需要调用 findDate 的文件顶部包含 .h 文件。
这是执行此操作的一般过程。
创建一个名为 Main.h 的文件:
#pragma once // Include guard, so you don't include multiple times.
// Declaration (it is okay to have multiple declarations, if they
// have a corresponding definition somewhere)
date findDate (void);
主.cpp:
// Definition (it is not okay to have multiple non-static definitions)
date
findDate (void)
{
// Do some stuff
return something;
}
日期.cpp
#include "Main.h"
date
Dates::SomeFunction (void)
{
return ::findDate ();
}
永远不要包含"Main.cpp",这将创建findDate (...)
函数的多个实现和符号(假设该函数未声明为static
),并且链接器将无法确定要链接到哪个输出对象。这称为符号冲突或多重定义错误。
相关文章:
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 未声明的标识符和类型"int"从 main 调用类时出现意外错误
- 如何使 WaitForSingleObject 在从 main 调用的线程内接收信号作为类成员函数
- C++尝试从main调用函数
- 如何用相同的签名从main调用基类函数
- C++ 如何从 main 调用派生的基类函数
- 读取函数由 main 调用,但不执行
- 如果方法私有,是否可以从 main 调用该方法?如果不是,怎么可能
- 使用类通过main调用函数
- 如果没有返回,如何向main调用void函数
- 在C++中从Main调用函数
- C++ 从实现文件中的 main 调用函数
- 从main调用c++源文件中的函数
- 如何在另一个包含对象的文件中创建一个函数并从 main 调用它
- 是否可以从常规main调用WinMain ?
- c++:从main()调用模板化函数
- 从c++中的main调用类中的结构体
- 在类中从main调用函数
- 未在作用域内声明的c++类——从main调用
- 如何编写函数并从main调用它们