我无法让接线员[]上班

I cannot get operator[] to work

本文关键字:上班 接线员      更新时间:2023-10-16

我有一个数据类,如下所示:

#ifndef DATA_HPP
#define DATA_HPP
#include "utils.hpp"
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
using namespace std;
namespace TSPGA {
    class data
    {
        /**
         * @brief The data coordinates
         */
        coordinates* _data;
    protected:
        /**
         * @brief construct an empty data(needed for (de)serialization)
         */
        data() {}
    public:
        /**
         * @brief construct a data of coordinates
         */
        data(coordinates* _data) : _data(_data) { }
        /**
         * @brief ~data
         */
        virtual ~data() { delete this->_data; }
        /**
         * @brief Get a data at an index
         * @param index The index of datas
         */
        inline TSPGA::coordinate operator[] (size_t index) const { return this->_data->at(index); }
        /**
         * @brief Get size of data
         */
        inline size_t size() const { return this->_data->size(); }
        /**
         * Serialization point
         */
#pragma GCC diagnostic ignored "-Wunused-parameter"
        friend class boost::serialization::access;
        template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar  & _data; }
#pragma GCC diagnostic pop
        /**
         * @brief Deserializes a data instance from a file
         * @param file The data file
         * @return The data instance
         */
        static data* deserialize(const char* file) {
            // create data instance
            data* _data = new data();
            // open the input file
            std::ifstream ifs( file );
            // bind the input file to an archiver
            boost::archive::text_iarchive ar( ifs );
            // deserialize the data
            ar & _data;
            // close input file
            ifs.close();
            // return the deserialized data
            return _data;
        }
        /**
         * @brief Serializes passed data into a file
         * @param _data The data to serialize
         * @param file The data file
         */
        static void serialize(const data* const _data, const char* file) {
            // open the output file
            std::ofstream ofs(file);
            // bind the output file to an archiver
            boost::archive::text_oarchive ar(ofs);
            // deserialize the data
            ar & _data;
            // close output file
            ofs.close();
        }
    };
}
#endif // DATA_HPP

正如你所看到的,我已经定义了一个inline TSPGA::coordinate operator[](int),当我尝试在中const coordinate x = d->_data[0];

bool test_allocation(data*& d) {
    IS_NULL(d);
    d = new data(new coordinates({ coordinate(1, 2), coordinate(3, 4) }));
    SHOULD_BE(d->size(), 2);
    const coordinate x = d[0];
    SHOULD_BE(x.longitude, 1);
    SHOULD_BE(x.latitude , 2);
}

我得到以下错误:

In file included from <PATH>/ga.tsp/test/manifest.hpp:15:0,
                 from <PATH>/ga.tsp/test/testerMain.cpp:12:
<PATH>/ga.tsp/test/TestCases/dataTestCase.hpp: In member function ‘bool CPP_TESTER::TESTS::dataTestCase::test_allocation(data*&)’:
<PATH>/ga.tsp/test/TestCases/dataTestCase.hpp:39:37: error: conversion from ‘data {aka TSPGA::data}’ to non-scalar type ‘const TSPGA::coordinate’ requested
             const coordinate x = d[0];
                                     ^
make[2]: *** [CMakeFiles/test.dir/testerMain.cpp.o] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

我不知道我做错了什么
我缺了什么吗?

如有任何帮助,我们将不胜感激。提前感谢


编辑

coordinatecoordinate的定义如下

struct coordinate
{
  double longitude, latitude;
  /**
   * @brief Init coordinate
   */
  coordinate() : longitude(-1), latitude(-1) { /* ctor */ }
  coordinate(double longitude, double latitude) : longitude(longitude), latitude(latitude) { /* ctor */ }
  /**
   * Serialization point
   */
  #pragma GCC diagnostic ignored "-Wunused-parameter"
      friend class boost::serialization::access;
      template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar  & longitude & latitude; }
  #pragma GCC diagnostic pop
};
typedef vector<coordinate> coordinates; 

test_allocation中,d参数是一个指针。在调用[]运算符之前,您必须取消引用指针,目前编译器认为您正在将指针用作数组查找。

尝试:

coordinate c = (*d)[0];