1
在模板類中,我有一個函數將numb-elements數組插入另一個數組中選定的索引。獲取錯誤「binary」[':'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);
}
典型的類型定義,當我把我的另一個類類型mcoord,我得到一個錯誤
error C2676: binary '[' : 'const typ' does not define this operator or a conversion to a type acceptable to the predefined operator
mcoord.h
#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();
};
並且當我定義典型值作爲INT I得到錯誤
error C2109: subscript requires array or pointer type
這兩個錯誤是在模板類與OB行[i]和OB [J ++] 。 我找不到任何解決方案,所以我會非常感謝您的幫助。
它的工作,非常感謝。 – user5857084