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

发布于 2024-08-10 17:22:56 字数 2338 浏览 4 评论 0原文

我有这个类

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();
};

}

,这是 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];
}

(是的,我确实意识到我不检查 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;
}

编译器说

../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

引用行 gv[i] = x; (好像没有看到[]的重新定义)

为什么???我缺少什么?


更正构造函数问题后,我让链接器说:

/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()'

就像它无法链接......为什么??? :哦

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[]

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:

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; (seems like it doesn't see the redefinition of [])

Why???? What am I missing?


After correcting the constructor problem I've the linker 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

权谋诡计 2024-08-17 17:22:56

问题在于你的声明

growVector<char> gv();

编译器将其解释为声明一个名为 gv 的函数,该函数返回一个 growVector,而不是你想要的对象。由于没有默认构造函数,因此无论如何都无法编译。将其更改为:

growVector<char> gv(0,0);

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. Since there isn't a default constructor, this wouldn't compile anyway. Change it to:

growVector<char> gv(0,0);
神经暖 2024-08-17 17:22:56

编译器认为这一行

growVector<char> gv();

声明的是函数,而不是变量。删除 () 就可以了。

The compiler thinks this line

growVector<char> gv();

is declaring a function, rather than a variable. Drop the () and things should work.

如歌彻婉言 2024-08-17 17:22:56

我只是想指出,在类中使用两个版本的下标 [] 运算符是一个很好的做法:const(将用于 r 值)和非 const。
您已经实现了非 const 版本,但它不能在 const 函数或任何接收类实例作为 const 引用或指向 const 的指针的函数中使用。

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.
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文