dimanche 5 novembre 2017

Why am I violating constness [duplicate]

This question already has an answer here:

gcc 6.4.0 Netbeans 8.2 Win7-64 cygwin 2.9.0

I don't understand why I am violating constness. Can you identify the issue please?

I can't find a const in Vector::operator[] which would lead to a const violation. The const in NodeDataClass:toString() I would think should have no effect. This is my first experience with templates so the only thing that I can think of is that there is something in template usage which causes an implicit cnost, but I have experimented with a simpler case without an issue. So I am confused as to what the error means or what to do to correct it.

If the architecture is wrong, what should it be?

------------------ code ------------------

#ifndef AUGMENTEDSTRING_H
#define AUGMENTEDSTRING_H

using namespace std;

class AugmentedString {
private:
   long length;                              //!< sizeof keyword
public:
   AugmentedString() : length(0){};
}; // AugmentedString


#endif /* AUGMENTEDSTRING_H */


#ifndef NODECLASS_H
#define NODECLASS_H

# include <sstream>

# include "AugmentedString.h"
# include "Vector.h"

using namespace std;

class NodeDataClass{
   Vector<AugmentedString*> keywords; 
public:
   NodeDataClass();
   virtual string toString() const {
      stringstream pretty;
      AugmentedString*  x = *keywords[0];
      AugmentedString** y = keywords[0];
      pretty << *keywords[0].toString() << endl;
      return pretty.str(); };
};

#endif  /* NODECLASS_H */


#ifndef VECTOR_H
#define VECTOR_H

using namespace std;

template <typename T>
class  Vector {
   private:
      T*    _ptr; 
      long  _ndx;
   public:
      Vector(T* ptr) : _ptr(ptr) { }    
      ~Vector() { }
      T*   operator[]( long ndx) {return (_ptr + ndx); }
};

#endif   // VECTOR_H

# include <iostream>

# include "Vector.h"
# include "NodeDataClass.h"

using namespace std;

int main(int argc, char** argv) {
   NodeDataClass node();
}

=========================== diagnostic messages ===========================

> In file included from main.cpp:5:0: NodeDataClass.h: In member
> function 'virtual std::string NodeDataClass::toString() const':
> NodeDataClass.h:18:40: error: passing 'const Vector<AugmentedString*>'
> as 'this' argument discards qualifiers [-fpermissive]
>        AugmentedString*  x = *keywords[0];
>                                         ^ In file included from main.cpp:4:0: Vector.h:15:12: note:   in call to 'T*
> Vector<T>::operator[](long int) [with T = AugmentedString*]'
>        T*   operator[]( long ndx) {return (_ptr + ndx); }
>             ^~~~~~~~ In file included from main.cpp:5:0: NodeDataClass.h:19:39: error: passing 'const Vector<AugmentedString*>'
> as 'this' argument discards qualifiers [-fpermissive]
>        AugmentedString** y = keywords[0];
>                                        ^ In file included from main.cpp:4:0: Vector.h:15:12: note:   in call to 'T*
> Vector<T>::operator[](long int) [with T = AugmentedString*]'
>        T*   operator[]( long ndx) {return (_ptr + ndx); }
>             ^~~~~~~~ In file included from main.cpp:5:0: NodeDataClass.h:20:28: error: passing 'const Vector<AugmentedString*>'
> as 'this' argument discards qualifiers [-fpermissive]
>        pretty << *keywords[0].toString() << endl;
>                             ^ In file included from main.cpp:4:0: Vector.h:15:12: note:   in call to 'T* Vector<T>::operator[](long int)
> [with T = AugmentedString*]'
>        T*   operator[]( long ndx) {return (_ptr + ndx); }
>             ^~~~~~~~ In file included from main.cpp:5:0: NodeDataClass.h:20:30: error: request for member 'toString' in
> '((const
> NodeDataClass*)this)->NodeDataClass::keywords.Vector<T>::operator[]<AugmentedString*>(0l)', which is of non-class type 'AugmentedString**'
>        pretty << *keywords[0].toString() << endl;
>                               ^~~~~~~~

Aucun commentaire:

Enregistrer un commentaire