在C 中,如何将其作为成员变量构建数据结构

In c++, how do I build a data structure with objects as member variables?

本文关键字:成员 变量 数据结构 构建      更新时间:2023-10-16

我最近开始研究,该研究涉及我完全不熟悉的CPP编码,并且遇到了很多东西:

  1. 默认构造函数
  2. 以对象作为成员变量创建数据结构。

我的程序设置需要这样:

  • 我需要创建一个具有其成员变量之一的数据结构,作为Wave对象的数组:Wave []
  • 实际上是从波浪文件中读取波浪的(我的代码链接到某些外部库来执行此操作(
  • 每个波都有一个GPS对象。
  • 每个GPS对象都有〜15个成员变量INT类型或double

这就是我的GPS.HPP课程的样子 -

 #ifndef GPS_HPP_
    #define GPS_HPP_
    #include <iostream>
    #include <fstream>
    #include "pulsereader.hpp"
    #include "pulsewriter.hpp"
    class GPS{
    public:
      //possible parameters
      double gpsTime;
      double xAnchor, yAnchor, zAnchor;
      double xTarget, yTarget, zTarget;
      double xDeviation, yDeviation, zDeviation;
      double xFirst, yFirst, zFirst;
      double xLast, yLast, zLast;
      unsigned char edge;
      unsigned char facet;
      unsigned char scanDirection;
      unsigned char intensity;
      PULSEreadOpener pOpener;
      PULSEreader *pReader;
      PULSEscanner scanner;
      GPS();
      void setGPSInformation();
      void writeToFileGPSInformation(std::string fileName);
    };
    #endif /* GPS_HPP_ */ 
This is my GPS.cpp class: 
    #include <iostream>
#include "GPS.hpp"
//Default constructor
GPS::GPS(){
  // enter default values
  xAnchor = 0;
  yAnchor = 0;
  zAnchor = 0;
  xTarget = 0;
  yTarget = 0;
  zTarget = 0;
  xFirst = 0;
  yFirst = 0;
  zFirst = 0;
  xLast = 0;
  yLast = 0;
  zLast = 0;
  edge = 0;
  facet = 0;
  scanDirection = 0;
  intensity = 0;
}

void GPS::setGPSInformation(){
  gpsTime = pReader->pulse.get_t();
  // Compute anchor, target and direction
  pReader->pulse.compute_anchor_and_target_and_dir();
  xAnchor = pReader->pulse.get_anchor_x();
  yAnchor = pReader->pulse.get_anchor_y();
  zAnchor = pReader->pulse.get_anchor_z();
  xTarget = pReader->pulse.get_target_x();
  yTarget = pReader->pulse.get_target_y();
  zTarget = pReader->pulse.get_target_z();
  // Compute first and last returning Values
  pReader->pulse.compute_first_and_last();
  xFirst = pReader->pulse.get_first_x();
  yFirst = pReader->pulse.get_first_y();
  zFirst = pReader->pulse.get_first_z();
  xLast = pReader->pulse.get_last_x();
  yLast = pReader->pulse.get_last_y();
  zLast = pReader->pulse.get_last_z();
  edge = pReader->pulse.edge_of_scan_line;
  scanDirection = pReader->pulse.scan_direction;
  facet = pReader->pulse.mirror_facet,
  intensity = pReader->pulse.intensity;
}
/*
 * Writes all GPS information to a csv file
 */
void GPS::writeToFileGPSInformation(std::string fileName){
  long long pulseIndex = 0;
  FILE *scanout;
  scanout = fopen("gps.csv", "w");
  fprintf(scanout, "Pulse Index, GPS Time, X Anchor, Y Anchor,  Z Anchor, 
                    X Target, Y Target, Z Target, X First, 
                    Y First, Z First, X Last, Y Last, Z Last, 
                    edge, Scan Direction, facet, intensityn");
  pOpener.set_file_name(fileName.c_str());
  pReader = pOpener.open();
  pReader->seek(0);
  while(pReader->read_pulse()) {
    gpsTime = pReader->pulse.get_t();
    pReader->pulse.compute_anchor_and_target_and_dir();
    xAnchor = pReader->pulse.get_anchor_x();
    yAnchor = pReader->pulse.get_anchor_y();
    zAnchor = pReader->pulse.get_anchor_z();
    xTarget = pReader->pulse.get_target_x();
    yTarget = pReader->pulse.get_target_y();
    zTarget = pReader->pulse.get_target_z();
    pReader->pulse.compute_first_and_last();
    xFirst = pReader->pulse.get_first_x();
    yFirst = pReader->pulse.get_first_y();
    zFirst = pReader->pulse.get_first_z();
    xLast = pReader->pulse.get_last_x();
    yLast = pReader->pulse.get_last_y();
    zLast = pReader->pulse.get_last_z();
    edge = pReader->pulse.edge_of_scan_line;
    scanDirection = pReader->pulse.scan_direction;
    facet = pReader->pulse.mirror_facet,
    intensity = pReader->pulse.intensity;
    fprintf(scanout, "%lld,%.8lf,   
                      %lf,%lf,%lf,  
                      %lf,%lf,%lf,  
                      %lf,%lf,%lf,  
                      %lf,%lf, %lf, 
                      %d,%d,%d,%d,n", 
            pulseIndex, gpsTime, 
            xAnchor, yAnchor, zAnchor, 
            xTarget, yTarget, zTarget,
            xFirst, yFirst, zFirst,
            xLast, yLast, zLast, 
            edge, scanDirection, facet, intensity) ;
    pulseIndex++;
  }
}

这是我的gps.cpp:

#include <iostream>
#include "GPS.hpp"
//Default constructor
GPS::GPS(){
  // enter default values
  xAnchor = 0;
  yAnchor = 0;
  zAnchor = 0;
  xTarget = 0;
  yTarget = 0;
  zTarget = 0;
  xFirst = 0;
  yFirst = 0;
  zFirst = 0;
  xLast = 0;
  yLast = 0;
  zLast = 0;
  edge = 0;
  facet = 0;
  scanDirection = 0;
  intensity = 0;
}

void GPS::setGPSInformation(){
  gpsTime = pReader->pulse.get_t();
  // Compute anchor, target and direction
  pReader->pulse.compute_anchor_and_target_and_dir();
  xAnchor = pReader->pulse.get_anchor_x();
  yAnchor = pReader->pulse.get_anchor_y();
  zAnchor = pReader->pulse.get_anchor_z();
  xTarget = pReader->pulse.get_target_x();
  yTarget = pReader->pulse.get_target_y();
  zTarget = pReader->pulse.get_target_z();
  // Compute first and last returning Values
  pReader->pulse.compute_first_and_last();
  xFirst = pReader->pulse.get_first_x();
  yFirst = pReader->pulse.get_first_y();
  zFirst = pReader->pulse.get_first_z();
  xLast = pReader->pulse.get_last_x();
  yLast = pReader->pulse.get_last_y();
  zLast = pReader->pulse.get_last_z();
  edge = pReader->pulse.edge_of_scan_line;
  scanDirection = pReader->pulse.scan_direction;
  facet = pReader->pulse.mirror_facet,
  intensity = pReader->pulse.intensity;
}
/*
 * Writes all GPS information to a csv file
 */
void GPS::writeToFileGPSInformation(std::string fileName){
  long long pulseIndex = 0;
  FILE *scanout;
  scanout = fopen("gps.csv", "w");
  fprintf(scanout, "Pulse Index, GPS Time, X Anchor, Y Anchor,  Z Anchor, 
                    X Target, Y Target, Z Target, X First, 
                    Y First, Z First, X Last, Y Last, Z Last, 
                    edge, Scan Direction, facet, intensityn");
  pOpener.set_file_name(fileName.c_str());
  pReader = pOpener.open();
  pReader->seek(0);
  while(pReader->read_pulse()) {
    gpsTime = pReader->pulse.get_t();
    pReader->pulse.compute_anchor_and_target_and_dir();
    xAnchor = pReader->pulse.get_anchor_x();
    yAnchor = pReader->pulse.get_anchor_y();
    zAnchor = pReader->pulse.get_anchor_z();
    xTarget = pReader->pulse.get_target_x();
    yTarget = pReader->pulse.get_target_y();
    zTarget = pReader->pulse.get_target_z();
    pReader->pulse.compute_first_and_last();
    xFirst = pReader->pulse.get_first_x();
    yFirst = pReader->pulse.get_first_y();
    zFirst = pReader->pulse.get_first_z();
    xLast = pReader->pulse.get_last_x();
    yLast = pReader->pulse.get_last_y();
    zLast = pReader->pulse.get_last_z();
    edge = pReader->pulse.edge_of_scan_line;
    scanDirection = pReader->pulse.scan_direction;
    facet = pReader->pulse.mirror_facet,
    intensity = pReader->pulse.intensity;
    fprintf(scanout, "%lld,%.8lf,   
                      %lf,%lf,%lf,  
                      %lf,%lf,%lf,  
                      %lf,%lf,%lf,  
                      %lf,%lf, %lf, 
                      %d,%d,%d,%d,n", 
            pulseIndex, gpsTime, 
            xAnchor, yAnchor, zAnchor, 
            xTarget, yTarget, zTarget,
            xFirst, yFirst, zFirst,
            xLast, yLast, zLast, 
            edge, scanDirection, facet, intensity) ;
    pulseIndex++;
  }
}

这是我目前用来将GPS信息写入CSV的驱动程序类。

#include <iostream>
#include "CmdLine.hpp"
#include "ScannerInformation.hpp"
#include "GPS.hpp"

using namespace std;
int main (int argc, char *argv[]){
  CmdLine cmdLineArgs;
  cmdLineArgs.parse(argc,argv);
  if(cmdLineArgs.printUsageMessage == true){
    std::cout << cmdLineArgs.getUsageMessage() << std::endl;
  }
  else{
    std::string fileName = cmdLineArgs.getInputFileName();
    ScannerInformation scannerInfo;
    scannerInfo.writeToFileScannerInformation(fileName);
    GPS gpsInfo;
    gpsInfo.writeToFileGPSInformation(fileName);
  }
  return 0;
}

除了将GPS信息写入CSV之外,我还希望能够访问我的驱动程序类中的每个浪潮的GPS信息(类似于此( -

std::cout << wave.gpsInfo.xAnchor;

如何编写Wave.HPP和CPP,以使其具有GPS对象作为其成员变量,然后如何从驱动程序文件中访问Wave对象的GPS信息?

#include "GPS.hpp"
class Wave {    
private:
  GPS gps;
public:
 //What kind of methods would I use here?
};

我要回答标题。

您只是这样做!一个例子:

struct Pos
{
    int x = 0;
    int y = 0;
};
struct Line
{
    Pos start;
    Pos end;
};

一些笔记:

  1. PosLine都是类。不要让关键字struct欺骗您。由于都没有类不变,因此他们的数据可以公开。
  2. 除非您必须编写构造函数。可以在课堂上完成成员的初始化。
  3. C 具有价值语义;这意味着类型通常行为像int一样,没有特殊的处理来区分对象和其他类型。

沿线的东西:

#include "GPS.hpp"
#include "OutgoingWave.hpp"
#include "IncomingWave.hpp"
class Wave {    
private:
  GPS _gps;
  OutgoingWave _ow;
  IncomingWave _iw;    
public:
  const GPS& gps() const { return _gps; }
  const OutgoingWave& ow() const { return _ow; }
  const IncomingWave& iw() const { return _iw; }
};

确保返回的引用不会超过Wave对象(如果是这样,则返回值,但会产生其他复制(。GPSIncomingWaveOutgoingWave类可以转换为struct S,以便默认公开访问所有成员。顺便说一句,您发布的代码不编译,publicprivate修饰符需要在类中。

要准确获取您在问题中要求的代码,您需要公开提供会员。最好使用一个结构来完成,因为默认情况下它是公开的。

struct Wave {
  GPS gpsInfo;
};
struct GPS {
  double gpsTime;
  double xAnchor; //you may want to use a Point class or struct
  ...
  unsigned char intensity;
};

这样,您就不需要任何额外的方法(即Getters and Setters(。这种方法没有错,但是您基本上失去了添加诸如SANITY检查输入的功能的可能性。如果Wave或GPS具有更复杂的状态,并且成员依赖于彼此的值,则可能需要使用类并使用Getters和Setters封装对成员的访问。