收到错误"二进制'[':"const typ"未定义此运算符"或"下标需要数组或指针类型



在一个模板类中,我有一个函数,可以在所选索引的另一个数组中插入一个麻元素数组。

template <class T>
void my_vect<T> ::insert(const T &ob, size_t ind, size_t numb)
{
    int i, j = 0;
    if (ind == last + 1)
    {
        for (i = 0; i < numb; i++)
            push(ob[i]);
    }
    else if (ind > last + 1)
        msg.mess(my_mess::WARN_ARR_SMALL);
    else
    {
        if (last + numb > ndim)
        {
            msg.mess(my_mess::WARN_ARR_FULL);
            realloc();
        }
        last += numb;
        for (i = last; i = (ind + numb); i--)
        {
            dat[i] = dat[i - numb];
        }
        for (i = ind; i < (ind + numb); i++)
            dat[i] = ob[j++];
    }
}

还有一个接口类,我在那里有一个管理插入功能

void my_interf::insertarr()
{
    typ *ob = NULL;
    size_t ind, numb, i;
    cout << "Podaj indeks: ";
    cin >> ind;
    cout << "Podaj ilosc elementow tablicy: ";
    cin >> numb;
    ob = new typ[numb];
    for (i = 0; i < numb; i++)
    {
        cout << "Podaj " << i << " element tablicy: " << endl;
        cin >> ob[i];
    }
    vect.insert(*ob, ind, numb);
}

typ是typedef,当我把我的另一个类类型mcoord时,我得到一个错误

error C2676: binary '[' : 'const typ' does not define this operator or a conversion to a type acceptable to the predefined operator

麦库德·

#pragma once
#include "stdafx.h"
#include "my_mess.h"
#include <iostream>
using namespace std;
class mcoord
{
protected:
    double *pcoord;
    my_mess msg;
public:
    mcoord(double xx, double yy);
    mcoord();
    mcoord(const mcoord &ob);
    ~mcoord() { delete[] pcoord; }
    friend ostream & operator << (ostream &strm, const mcoord &ob);
    friend istream & operator >> (istream &strm, mcoord &ob);
    mcoord & operator = (const mcoord &ob);
    int operator == (const mcoord &praw) const;
private:
    void alloc();
};

当我将 typ 定义为 int 时,我得到错误

error C2109: subscript requires array or pointer type   

这两个错误都在模板类中与 ob[i] 和 ob[j++] 一起出现。我找不到任何解决方案,所以我将非常感谢帮助。

您需要将函数声明为:

template <class T>
void my_vect<T> ::insert(const T *ob, size_t ind, size_t numb)

换句话说,使ob成为指针而不是引用。

相关内容

最新更新