gcc 6.4.0 Netbeans 8.2 Win7-64 cygwin 2.9.0
我不明白为什么我违反了constness。你能确定问题吗?
我在Vector :: operator []中找不到会导致const违规的const。我认为NodeDataClass:toString()中的const应该没有效果。这是我对模板的第一次体验,所以我唯一能想到的是模板使用中有一些东西会导致隐含的cnost,但我已经尝试了一个没有问题的简单案例。因此,我对错误的含义或纠正方法感到困惑。
如果架构错了,它应该是什么?
------------------ 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();
}
===========================诊断消息================== =========
> 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;
> ^~~~~~~~