简体   繁体   English

模板化的类无法重新定义运算符[]

[英]templated class can't redefine operator[]

I've this class 我上这堂课

namespace baseUtils {

template<typename AT>
class growVector {

        int size;
        AT **arr;
        AT* defaultVal;

    public:

        growVector(int size, AT* defaultVal );   //Expects number of elements (5) and default value (NULL)
        AT*& operator[](unsigned pos);
        int length();
        void reset(int pos);    //Resets an element to default value
        void reset();           //Resets all elements to default value
        ~growVector();
};

}

and this is the implementation for operator[] 这是operator []的实现

template<typename AT>
AT*& growVector<AT>::operator [](unsigned pos){
    if (pos >= size){
        int newSize = size*2;
        AT** newArr = new AT*[newSize];
        memcpy(newArr, arr, sizeof(AT)*size);
        for (int i = size; i<newSize; i++)
            newArr[i] = defaultVal;
        size = newSize;
        delete arr;
        arr = newArr;
    }
    return arr[pos];
}

(yes I do realize i don't check if size*2 >= pos... but that's not the point now) if I use it in code like: (是的,我的确意识到我不检查size * 2> = pos ...但这不是重点)如果我在代码中使用它,例如:

int main() {

    growVector<char> gv();
    char* x = NULL;
    for (int i = 0; i< 50; i++){
        gv[i] = x;
    }
    gv.reset();
    return 0;
}

the compiler says 编译器说

../src/base.cpp:98: warning: pointer to a function used in arithmetic
../src/base.cpp:98: error: assignment of read-only location ‘*(gv + ((unsigned int)i))’
../src/base.cpp:98: error: cannot convert ‘char*’ to ‘baseUtils::growVector<char>()’ in assignment

referring to the line gv[i] = x; 参照线gv [i] = x; (seems like it doesn't see the redefinition of []) (似乎没有看到[]的重新定义)

Why???? 为什么???? What am I missing? 我想念什么?


After correcting the constructor problem I've the linker sayng: 更正构造函数问题后,我有了链接器sayng:

/home/dario/workspace/base/Debug/../src/base.cpp:95: undefined reference to `baseUtils::growVector<char>::growVector(int, char*)'
/home/dario/workspace/base/Debug/../src/base.cpp:98: undefined reference to `baseUtils::growVector<char>::operator[](unsigned int)'
/home/dario/workspace/base/Debug/../src/base.cpp:100: undefined reference to `baseUtils::growVector<char>::reset()'
/home/dario/workspace/base/Debug/../src/base.cpp:101: undefined reference to `baseUtils::growVector<char>::~growVector()'
/home/dario/workspace/base/Debug/../src/base.cpp:101: undefined reference to `baseUtils::growVector<char>::~growVector()'

like it cannot link... why??? 像它无法链接...为什么? :O :O

The problem is your declaration 问题是你的声明

growVector<char> gv();

The compiler interprets this as declaring a function called gv which returns a growVector<char> , not as an object as you indend. 编译器将此解释为声明了一个名为gv的函数,该函数返回growVector<char> ,而不是您所追求的对象。 Since there isn't a default constructor, this wouldn't compile anyway. 由于没有默认的构造函数,因此无论如何都不会编译。 Change it to: 更改为:

growVector<char> gv(0,0);

The compiler thinks this line 编译器认为这一行

growVector<char> gv();

is declaring a function, rather than a variable. 在声明一个函数,而不是一个变量。 Drop the () and things should work. 删除() ,一切正常。

I just would like to point out that it is a good practice to have two versions of subscript [] operator in the class: const (which will be used for r-value) and non-const. 我只想指出,在类中有两个版本的下标[]运算符是一种好习惯:const(将用于r值)和non-const。 You have implemented non-const version but it can not be used in const functions or in any function that receive instance of your class as const reference or pointer pointer to const. 您已经实现了非const版本,但不能在const函数或任何将类实例作为const引用或指向const的指针的类中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM