0

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;
>                               ^~~~~~~~
lostbits
  • 928
  • 1
  • 9
  • 23
  • You need to make `Vector::operator[]` also be `const`. Otherwise you are attempting to modify a class member from a const member function. – M.M Nov 05 '17 at 22:02
  • BTW consider using `vector` instead, what you have is going to get messy real fast. – M.M Nov 05 '17 at 22:03
  • @brian Thanks. Please give me the duplicate question reference. Const won't work The abbreviated example does not include automatic list expansion in Vector::operator[]. As to 'ugly' I agree. My issue was to preserve pointers rather than copy data because the input data is unknown. I would have preferred to use vector but I ran into some difficulty and resolved that with Vector.h. New error "request for member 'toString' ... operator[](0l)', which is of non-class type 'AugmentedString** – lostbits Nov 05 '17 at 23:04
  • The duplicate link is at the top of the page – Brian Bi Nov 05 '17 at 23:04
  • The issue in the reference is "...calling a non-const member function on const object...". The issue here is "referencing a non-const object in a const function". Is this the same? – lostbits Nov 05 '17 at 23:26
  • @brian. Additional code in the previous question: inline bool operator< (const StudentT & s1, const StudentT & s2) { return s1.getId() < s2.getId(); }" so it is possible to reference a non-const function from a const function, albeit the input arguments are const. I am trying to do reference a non-const function from a const function but am getting an error. – lostbits Nov 05 '17 at 23:36
  • @ArthurSchwarez when you call a const member function, it only has const access to its members. So if it then tries to call non-const functions on a member, it's calling a non-const function on a const expression. So it's the same problem, it's just a little better hidden here. – Brian Bi Nov 05 '17 at 23:39
  • @brian: I wish you hadn't said that. Sigh. I understand and thanks. – lostbits Nov 06 '17 at 00:18

0 Answers0