Je suis complètement nouveau en C++ et j'ai un problème très stupide.Problème avec les qualificateurs const pour obtenir les attributs privés d'un objet
J'ai une classe Graph et j'ai besoin de créer un constructeur de copie pour cela. Ceci est ma classe:
#include <igraph.h>
#include <iostream>
using namespace std;
class Graph {
public:
Graph(int N); // contructor
~Graph(); // destructor
Graph(const Graph& other); // Copy constructor
igraph_t * getGraph();
int getSize();
private:
igraph_t graph;
int size;
};
Il y a une fonction int igraph_copy(igraph_t * to, const igraph_t * from)
en igraph.h
que des copies d'une manière adéquate le type igraph_t
.
Le constructeur et destructor sont trivial et travailler normalement, et je le constructeur de copie suivante:
Graph :: Graph(const Graph& other) {
igraph_t * otherGraph = other.getGraph();
igraph_copy(&graph, otherGraph);
size = other.getSize();
}
igraph_t * Graph :: getGraph(){
return &graph;
}
int Graph :: getSize() {
return size;
}
Quand je compile, je suis les erreurs suivantes:
[email protected]:~/authC/teste$ make
g++ -I/usr/include/igraph -L/usr/local/lib -ligraph -c foo.cpp -o foo.o
foo.cpp: In copy constructor ‘Graph::Graph(const Graph&)’:
foo.cpp:30: error: passing ‘const Graph’ as ‘this’ argument of ‘igraph_t* Graph::getGraph()’ discards qualifiers
foo.cpp:32: error: passing ‘const Graph’ as ‘this’ argument of ‘int Graph::getSize()’ discards qualifiers
make: *** [foo.o] Error 1
Je pense que ce doit être quelque chose de très basique que je n'ai pas eu à propos de ce que le qualificatif const signifie.
Je ne connais pas vraiment le C++ (et je ne connais pas vraiment C profondément, d'ailleurs ...) mais j'ai besoin de jouer avec du code fait par des gens qui le font. :(
Des indices ou des commentaires sur ce constructeur de copie sera également très humblement apprécié. P