2015-03-17 2 views
-1

У меня есть класс данных, как показано ниже:я не могу получить оператор [], чтобы работать

#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 

Я не знаю, что я делаю неправильно!
Есть ли что-нибудь, что мне не хватает?

Любая помощь будет оценена по достоинству. Заранее спасибо


EDIT

coordinate Определения и coordinate как ниже

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

выглядит довольно понятно. Вы берете «данные» и пытаетесь инициализировать «координату». Это не имеет ничего общего с 'operator []'. –

ответ

3

В вашем test_allocation параметра d является указателем. Вы должны разыменовать свой указатель перед вызовом оператора [], в настоящее время компилятор считает, что вы используете указатель в качестве поиска массива.

Try:

coordinate c = (*d)[0]; 
+0

OMG, ПОЧЕМУ Я НЕ УВИДЕЛ, ЧТО !!! ?? : | – dariush

+1

Ну, вот что рассказал вам компилятор. –

+0

Я думал, что это связано с оператором [] – dariush

Смежные вопросы