在 .h 中声明名称,但在 Xcode 中以.cpp形式报告未声明
declared name in.h but reports undeclare in .cpp in Xcode
下面是我的代码:
MathCore.h
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
#include "math.h"
class MathCore
{
public:
MathCore();
virtual ~MathCore( );
int x (int n );
};
#endif
数学核心.cpp
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
#include "MathCore.h"
MathCore::MathCore()//a
{
}
MathCore::~ MathCore()//b
{
}
int MathCore::x (int n )//c
{
float v=0;
return v;
}
#endif
但它错误在
a:C++ requires a type specifier for all declarations
Use of undeclared identifier 'MathCore'
b:Expected a class or namespace
c:Expected a class or namespace
欢迎您的评论
.cpp 和 .h 文件中都不应该有该#define
,因为它会阻止实际包含 .h 文件的内容。
当您#include
文件时,出于所有实际目的,它的行为方式与将该文件复制并粘贴到您拥有#include
的任何位置相同。所以,你的MathCore的前几行.cpp实际上是这样的:
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
#ifndef __CC_MATHCORE_H__
#define __CC_MATHCORE_H__
/* the rest of your .h file here */
当以这种方式重组时,它会变得更加明显,第二个#ifndef
永远无法匹配,因为它正在检查的交易品种是在上面定义的。
因为,在C++文件中,您使用的标头保护与标头相同。第二行定义__CC_MATHCORE_H__
。在此之后,您将包含标头,如果定义了__CC_MATHCORE_H__
则不会执行任何操作。从 cpp 文件中删除防护装置,你会没事的。在实际实现文件中很少需要防护(如果有的话)。
//////////////////////////////////MathCore.cpp
#include "MathCore.h"
MathCore::MathCore()//a
{
}
MathCore::~ MathCore()//b
{
}
int MathCore::x (int n )//c
{
float v=0;
return v;
}
从 MathCore 中删除包含保护.cpp
相关文章:
- 内置函数可查看CPP中的成员变量
- 在Windows中以.exe的形式运行c++
- 我需要知道编译器如何在cpp中使用析构函数
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- 无法按cpp中的值返回矢量
- 使用cpp中的结构和函数的多个学生条目
- 什么是(或如何制作)cpp中矢量的默认初始值设定项?
- 如何在CPP中创建应该在Windows和Linux上运行的套接字?
- 如何在 cpp 中解析此文件?
- 如何在 cpp 中重载运算符 +=?
- 将自定义函数传递到基抽象类中以延迟执行
- 当我有多个类时,我应该以线性样式包含它们还是只包含在 main.cpp 中?
- 从 Python 中以“.cpp”格式保存 LGBM 模型
- 覆盖 CPP 中的链接以指向模拟实现
- 如何防止在标题中包含标头,以最大程度地减少包含在其他 CPP 中
- 如何在CPP中以最快的方式对HTML访问日志进行分类
- 在 .h 中声明名称,但在 Xcode 中以.cpp形式报告未声明
- 如何在cpp中创建一个固定大小(以内存为单位)的哈希表