常量、转换和泛型数组问题

发布于 2024-08-13 13:48:45 字数 2606 浏览 5 评论 0原文

我在这些行中遇到问题:

const int* index = faceArray[f].vertices;

const Vector3& A = vertexArray[index[0]];
const Vector3& B = vertexArray[index[1]];
const Vector3& C = vertexArray[index[2]];

faceNormal[f] = Vector3::Cross(B - A, C - A).Normalize();

当我尝试编译时,出现错误:

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const MyArray<Data>' (or there is no acceptable conversion)
with [Data=Vector3]
c:\...\projects\haziarnyek\hazi\hazi.cpp(502): could be 'Vector3 &MyArray<Data>::operator [](const int &)'
with [Data=Vector3]
while trying to match the argument list '(const MyArray<Data>, int)'
with [Data=Vector3]

faceArray[f].vertices is an int array。

而 vertexArray 是我写的一个通用数组,类似于:

class MyArray {
public:
    struct element {
        Data data;
        struct element* next;
    };
    element* list;
    element* last;
    int size;
    MyArray(){
        list = new element();
        list->next = NULL;
        last = list;
        size = 0;
    }
    MyArray(int size){
        this->size = size;
        element = new element();
        element* p = list;
        for(int i = 0; i < size; i++) {
            p->next = new element();
            p = p->next;
        }
        p->next = NULL;
        last = p;
    }
    MyArray(const MyArray& o){
        size = o.size;
        element * p = o.list->next;
        list = new element();
        element * p2 = list;
        while (p) {
            p2 = p2->next;
            p2 = new element();
            p2->data = p->data;
            p = p->next;
        }
        p2->next = NULL;
        last = p2;
    }
    Data& operator[](const int& i){
        if (i > size-1) {
            for (int j = 0; j < i-size+1; j++) Push();
            return last->data;
        }
        element* p = list->next;
        for (int j = 0; j < i; i++) {
            p = p->next;
        }
        return p->data;
    }
    void Push(const Data& d) {
        last->next = new element();
        last = last->next;
        last->next = NULL;
        last->data = d;
    }
    void Push() {
        last->next = new element();
        last = last->next;
        last->next = NULL;
    }
    ~MyArray() {
        element* p = list->next;
        element* p2;
        delete list;
        for(;p;) {
            p2 = p;
            p = p->next;
            delete p2;
        }
    }
};

I have a problem in these lines:

const int* index = faceArray[f].vertices;

const Vector3& A = vertexArray[index[0]];
const Vector3& B = vertexArray[index[1]];
const Vector3& C = vertexArray[index[2]];

faceNormal[f] = Vector3::Cross(B - A, C - A).Normalize();

When I try to compile, I get an error:

error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const MyArray<Data>' (or there is no acceptable conversion)
with [Data=Vector3]
c:\...\projects\haziarnyek\hazi\hazi.cpp(502): could be 'Vector3 &MyArray<Data>::operator [](const int &)'
with [Data=Vector3]
while trying to match the argument list '(const MyArray<Data>, int)'
with [Data=Vector3]

faceArray[f].vertices is an int array.

And vertexArray is a generic array that I wrote, similar to:

class MyArray {
public:
    struct element {
        Data data;
        struct element* next;
    };
    element* list;
    element* last;
    int size;
    MyArray(){
        list = new element();
        list->next = NULL;
        last = list;
        size = 0;
    }
    MyArray(int size){
        this->size = size;
        element = new element();
        element* p = list;
        for(int i = 0; i < size; i++) {
            p->next = new element();
            p = p->next;
        }
        p->next = NULL;
        last = p;
    }
    MyArray(const MyArray& o){
        size = o.size;
        element * p = o.list->next;
        list = new element();
        element * p2 = list;
        while (p) {
            p2 = p2->next;
            p2 = new element();
            p2->data = p->data;
            p = p->next;
        }
        p2->next = NULL;
        last = p2;
    }
    Data& operator[](const int& i){
        if (i > size-1) {
            for (int j = 0; j < i-size+1; j++) Push();
            return last->data;
        }
        element* p = list->next;
        for (int j = 0; j < i; i++) {
            p = p->next;
        }
        return p->data;
    }
    void Push(const Data& d) {
        last->next = new element();
        last = last->next;
        last->next = NULL;
        last->data = d;
    }
    void Push() {
        last->next = new element();
        last = last->next;
        last->next = NULL;
    }
    ~MyArray() {
        element* p = list->next;
        element* p2;
        delete list;
        for(;p;) {
            p2 = p;
            p = p->next;
            delete p2;
        }
    }
};

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

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

发布评论

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

评论(2

世界等同你 2024-08-20 13:48:45

你应该定义一个const版本的operator[]:

const x& operator[] (unsigned idx) const;

You should define a const version of operator[]:

const x& operator[] (unsigned idx) const;
最单纯的乌龟 2024-08-20 13:48:45

我认为您给出的第一个片段的上下文类似于

void f(const MyArray<Vector3> &vertexArray) {
    ...
}

该错误是因为 MyArray 没有为常量实例定义 operator[] 。您所拥有的不会执行此操作,因为它会根据需要增长实例,因此您需要填写以下内容并将其添加到 MyArray 中:

const Data& operator[] (int i) const {
    ...
}

I take it the context of the first snippet you gave resembles

void f(const MyArray<Vector3> &vertexArray) {
    ...
}

The error is because MyArray does not have an operator[] defined for constant instances. The one you have won't do because it grows instances as needed, so you'll need to fill in the following and add it to MyArray:

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