c++ - 无法在头文件 C++ 中声明类
问题描述
我的代码有点麻烦。我想在头文件中声明类并将其调用到我的 CPP 文件中。但我总是得到错误,'coba' 不是类或命名空间名称。
这是我的头文件,'testFile.hpp':
#pragma once
class coba
{
private:
int num;
public:
//coba();
int getNum();
};
这是我的 CPP 文件,“testFile.cpp”:
#include "testFile.hpp"
#include "pch.h"
int coba::getNum()
{
return 10;
}
解决方案
这有效:
头文件,'testFile.hpp':
#ifndef __coba__
#define __coba__
class Coba
{
private:
int num;
public:
Coba();
int getNum();
};
#endif
这是我的 CPP 文件,“testFile.cpp”:
#include "testFile.hpp"
Coba::Coba()
{
}
int Coba::getNum()
{
return 10;
}
推荐阅读
- c++ - 是否可以在不使用 C++ 创建窗口的情况下创建消息循环
- python - 通过同时使用 iloc 和布尔掩码设置数据帧(数据帧中多个不同索引(行)值的掩码)
- sorting - 使用延迟加载对动态填充的数据表进行排序
- html - 导航栏:主页在左侧,高度相同
- java - 将 RGB 转换为灰度
- c# - 为什么 QueueClient RegisterSessionHandler 停止工作?
- angularjs - 显示从当天到周末的日期
- android-emulator - android Pie模拟器中没有互联网
- php - TCPDF 上的 QR(write2DBarcode)代码
- c# - 从 WPF 桌面应用程序中的数据库中检索数据