Reputation: 165
I have a class CaloReader
that creates a pointer _calo
to an object called Calorimeter
containing a grid of cells with an ID number. This pointer is then used to set an ID number to each cell. Futhermore, I have a accessor const Calorimeter& calo()
that returns a reference of this pointer. Now, I want to create a function dumpReadoutMap()
that prints out all ID numbers of all cells. The function is called from my main file as follows:
#include <iostream>
#include "CaloCell.hh"
#include "CaloGrid.hh"
#include "Point.hh"
#include "Calorimeter.hh"
#include "CaloReader.hh"
int main(){
CaloReader r("calo.dat");
r.calo().dumpReadoutMap();
}
The problem with my code is that the complirer gives the following error:
error: ‘const class Calorimeter’ has no member named ‘dumpReadoutMap’
r.calo().dumpReadoutMap();
The only relevant file for this question is the below as the other ones are correct and cannot be changed.
#ifndef CALOREADER_HH
#define CALOREADER_HH
#include <iostream>
#include <fstream>
#include "Calorimeter.hh"
#include "CaloReaderException.hh"
#include <string>
#include <iomanip>
class CaloReader {
public:
CaloReader(const char* file): _file(file) {
Calorimeter* _calo = 0;
ifstream _file(file);
std::string word;
_file >> word;
_file >> word; //Reads in next word.
if(word=="SIZE") {
int size_x;
int size_y;
_file >> size_x;
_file >> size_y;
_calo = new Calorimeter(size_x,size_y);
}
_file >> word;
while(word=="POSITION") {
int readoutID;
int ix;
int iy;
_file >> readoutID >> ix >> iy;
//std::cout << word << " " << readoutID << " " << ix << " " << iy << std::endl;
_calo->grid().cell(ix,iy)->setID(readoutID);
_file >> word;
}
}
~CaloReader() {}
const Calorimeter& calo() const {
return *_calo;
}
void dumpReadoutMap(std::ostream& os = std::cout) {
for(int x =0; x<size_x; x++) {
for(int y=0; y<size_y; y++) {
os << std::setw(6) << grid().cell(x,y)->getID();
}
os << std::endl;
}
}
private:
Calorimeter* _calo;
std::ifstream _file;
std::string word;
};
#endif
Upvotes: 1
Views: 425
Reputation: 774
Since calo() returns a const reference, you can't call a non-const member function with it. It's disappointing the error message doesn't explain clearly what the error is.
Upvotes: 0
Reputation: 148900
dumpReadoutMap
is a method in CaloReader
class. So it must be called from a CaloReader
object. You should use:
CaloReader r("calo.dat");
r.dumpReadoutMap();
Upvotes: 1