gpt4 book ai didi

c++ - 我无法让运算符(operator)[]工作

转载 作者:行者123 更新时间:2023-12-02 11:07:39 25 4
gpt4 key购买 nike

我有一个如下的数据类:

#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),当我尝试在
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
我不知道我在做什么错!
我有什么想念的吗?
任何帮助,将不胜感激。
提前致谢

编辑 const coordinate x = d->_data[0];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;

最佳答案

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

尝试:

coordinate c = (*d)[0];

关于c++ - 我无法让运算符(operator)[]工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com