我无法让operator []工作

时间:2015-03-17 16:43:08

标签: c++ compiler-errors operators

我有一个如下数据类:

#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; 

1 个答案:

答案 0 :(得分:3)

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

尝试:

coordinate c = (*d)[0];