一、类的声明
//global.h -- define for all project
//version 0.0
#ifndef GLOBAL_H_ //防止重定义
#define GLOBAL_H_
class Global //定义类
{
public: //使用类对象的程序都可以直接访问公有部分
/*定义在类声明中的函数称为内联函数,仅声明在类中的函数不是内联函数,
但定义时加上"inline"就是内联函数:inline int Global::read_sound(){}*/
int read_sound(const char* fileName, char* sound, int len);
int read_gray_dat(const char* fileName, uint16_t** Gray, int frameNum, int len);
int read_depth_dat(const char* fileName, uint16_t** Depth, int frameNum, int len);
int read_cloud_dat(const char* fileName, float** PointCloud, int frameNum, int len);
int read_RGB_dat(const char* fileName, uint8_t** RGB, int frameNum, int len);
/*构造函数的作用:构造新对象、将值赋给他们的数据成员;构造函数名为Global(),
没有返回值,也没有声明类型;程序在声明对象时会自动调用构造函数*/
Global(int height, int width);
/*构造函数的作用:构造新对象、将值赋给他们的数据成员;构造函数名为~Global(),
没有返回值,也没有声明类型和参数;作用是完成清理工作,释放内存*/
~Global();
/*
const 成员函数:
当类的声明为:const Global obstacle = Global(480, 640); 时不能调用成员函数,因为不能保证私有成员数据
是否会被改变,若要被调用此时函数的声明应为:int read_sound(const char* fileName, char* sound, int len) const;
函数的定义为:int Global::read_sound(const char* fileName, char* sound, int len) const{}
*/
/*this指针:this指针指向调用对象*/
const Global& compare(const Global& s) const;
private: //访问私有部分只能通过公有成员函数或者友元函数,不写关键字的声明默认为是private
int m_height;
int m_width;
static const int Months = 12;//类中创建数组方式
double costs[Months];
protected:
};
#endif
二、成员函数的定义
//global.h -- define global function
//version 0.0
/*
定义成员函数时使用作用域解析运算符(::)来表示函数;
类方法可以访问类的private组件;
*/
#include <iostream>
#include "global.h"
using namespace std;
//构造函数
Global::Global(int height, int width) {
m_height = height;
m_width = width;
}
/*
构造函数定义时如果不设置参数数,则可以在声明对象时直接调用:Global obstacle;
Global::Global() {
m_height = 480;
m_width = 640;
}
*/
//析构函数,它的调用时间由编译器决定
Global::~Global() {
cout << "game over" << endl;
}
const Global& Global::compare(const Global& s) const
{
if (s.m_height > m_height) {
return s;
}
else
{
return *this;
}
}
//读取超声波数据
int Global::read_sound(const char* fileName, char* sound, int len)
{
if (NULL == fileName)
{
return 0;
}
//--------------------------------------------------------------------------------
//open file
FILE* fFileID = fopen(fileName, "rb");
if (NULL == fFileID)
{
cout << "Can't open the file." << endl;
exit(EXIT_FAILURE);
}
fread(sound, sizeof(char), len, fFileID);
fclose(fFileID);
return 1;
}
三、类的调用
#include "global.h"
#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>
#include <opencv2\opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\opencv.hpp>
int main() {
//初始化全局参数
char CloudFile[256] = { 0 };
char GrayFile[256] = { 0 };
char RGBFile[256] = { 0 };
char UsoundFile[256] = { 0 };
char DepthFile[256] = { 0 };
int width = 320;
int height = 240;
int len = width * height;
int frameNum = 20;
//开辟多帧全局空间
char* sound = (char*)malloc(sizeof(char) * 3); //单帧超声波数据
uint16_t* GrayData = (uint16_t*)malloc(sizeof(uint16_t) * len * frameNum);
float* CloudData = (float*)malloc(sizeof(COOR3D32F) * len * frameNum);
uint16_t* DepthData = (uint16_t*)malloc(sizeof(uint16_t) * len * frameNum);
uint8_t* RGBData = (uint8_t*)malloc(sizeof(uint8_t) * len * frameNum * 1.5);
uint16_t** Gray_raw = (uint16_t**)malloc(sizeof(uint16_t*) * frameNum);
uint16_t** Depth_raw = (uint16_t**)malloc(sizeof(uint16_t*) * frameNum);
float** Point_raw = (float**)malloc(sizeof(float*) * frameNum);
uint8_t** RGB_raw = (uint8_t**)malloc(sizeof(uint8_t*) * frameNum);
//Mat img(200, 200, CV_8UC1);
//img = Thred();
int data_long = 3 * len;
for (int i = 0; i < frameNum; i++)
{
Gray_raw[i] = (uint16_t*)(&GrayData[i * len]);
Depth_raw[i] = (uint16_t*)(&DepthData[i * len]);
Point_raw[i] = (float*)(&CloudData[i * 4 * len]);
RGB_raw[i] = (uint8_t*)(&RGBData[i * data_long]);
}
char path[] = "E:\\check_GitCode\\obstacle\\XB_test_bug_data\\室内\\室内-10x5cm_低反\\室内-10x5低反-1421mm(及之后距离)识别不到\\";
char point[] = "point_cloud.dat";
char usound[] = "usound.dat";
//char gray[] = "gray.dat";
char depth[] = "depth.dat";
//char rgb[] = "RGB_DATA.dat";
sprintf(CloudFile, "%s%s", path, point);
//sprintf(RGBFile, "%s%s", path, rgb);
//sprintf(GrayFile, "%s%s", path, gray);
sprintf(DepthFile, "%s%s", path, depth);
sprintf(UsoundFile, "%s%s", path, usound);
Global obstacle = Global(480, 640); //声明一个Global类对象,叫obstacle,显式调用构造函数
//也可也用列表初始化:Global obstacle = {480, 640}; Global avoid{480};
Global avoid(480, 640); //声明一个Global类对象,叫avoid,隐式调用构造函数
Global* pstock = new Global(480, 640); //使用new构造函数,这种情况下对象没有名称,使用pstock指针来管理对象
obstacle.read_sound(UsoundFile, sound, 3); //成员函数调用
obstacle.read_cloud_dat(CloudFile, Point_raw, frameNum, len);
obstacle.read_depth_dat(DepthFile, Depth_raw, frameNum, len);
//read_RGB_dat(RGBFile, RGB_raw, frameNum, width, height, data_long);
//read_gray_dat(GrayFile, Gray_raw, frameNum, width, height, len);
/*对象数组:一次声明多个类对象*/
const int num = 4;
Global ob_avoid[num] = {
Global(480, 320),
Global(480, 320),
Global(480, 320),
Global(480, 320)
};
ob_avoid[0].read_cloud_dat(CloudFile, Point_raw, frameNum, len);