在c ++中,如何使用对象作为成员变量构建数据结构?

时间:2017-06-27 07:10:23

标签: c++ object data-structures constructor

我最近开始研究涉及cpp编码,这对我来说是全新的,并且遇到了很多问题:

  1. 默认构造函数
  2. 使用对象作为成员变量创建数据结构。
  3. 我的程序设置必须如下:

    • 我需要创建一个数据结构,其中一个成员变量为Wave对象数组:Wave []
    • Wave实际上是从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, intensity\n");
    
      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, intensity\n");
    
      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之外,我希望能够在我的驱动程序类中访问每个Wave的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?
    };
    

3 个答案:

答案 0 :(得分:2)

我要回答标题。

你就是这样做的!一个例子:

struct Pos
{
    int x = 0;
    int y = 0;
};

struct Line
{
    Pos start;
    Pos end;
};

一些注意事项:

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

答案 1 :(得分:0)

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

struct Wave {
  GPS gpsInfo;
};

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

这样你就不需要任何额外的方法(即吸气剂和固定剂)。这种方法没有任何问题,但您基本上失去了添加诸如输入的健全性检查等功能的可能性。如果Wave或GPS具有更复杂的状态且成员依赖于彼此的值,您可能希望使用类并使用getter和setter封装对其成员的访问。

答案 2 :(得分:0)

有些事情:

#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,以便对所有成员进行默认公共访问。顺便说一句,您发布的代码无法编译,publicprivate修饰符需要在一个类中。