Getting "instantiated from here" error while working with templates and classes

I'm in a class learning C++ and our latest project includes using templates in conjunction with classes, arrays, operator overloads and some other junk. I have little to no idea what I'm doing. I got this error first on my += operator but couldn't figure out how to fix it, so I commented that out and moved onto the next operator. But then I got the same error on that one, which leads me to believe it's something I'm doing wrong without even knowing it's wrong.

Edit: I should probably mention that large parts of the code were written for us by our professor, we have to edit the code to allow for the fact that an array's capacity may not be fully used, and to allow for the subscript operator to change elements outside of the normal capacity by "expanding" the array by creating a new one and copying.

The specific error code is:

In file included from atest.cpp:14:
Array.h: In member function ‘dataType& Array<dataType>::operator[](int) [with dataType = int]’:
atest.cpp:37: instantiated from here
Array.h:142: error: no match for ‘operator*’ in ‘**(((Array<int>*)this) + ((long unsigned int)(((long unsigned int)i) * 16ul)))’
Array.h:143: error: request for member ‘capacity’ in ‘temp’, which is of non-class type ‘int*’
Array.h:144: error: request for member ‘numUsed’ in ‘temp’, which is of non-class type ‘int*’

but the last 2 errors about member capacity is new for the second operator I tried, before it was just the first two

Here is the section of the .cpp file that's testing the header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// create an empty Array object 
  cout << "Default Constructor" << endl;
  Array<int> iList1(3);
  printArray(iList1);

  // test += on object that is not at capacity
  cout << "Add an item (under capacity)" << endl;
  //iList1 += 3;
  printArray(iList1);


  // change the last value
  cout << "Change last value" << endl;
  iList1[iList1.getNumUsed()] = 6;
  printArray(iList1);


the code from the header file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Append element to array
template <class dataType> 
Array<dataType> &Array<dataType>::operator+=(dataType elt) {
 /* if(capacity > numUsed)
  {
	  *this[numUsed+1] = elt;
	  numUsed++;
	  return(*this);
  }
  else
  {
	dataType *temp = new dataType[capacity+1];
	for (int i = 0; i < capacity; ++i) 
		temp[i]=ptr[i];
	temp[capacity++] = elt;
	delete [] ptr;
	ptr = temp;
	numUsed = capacity;
	return(*this);
  }*/
}

// Overloaded subscript operator for non-const Arrays
template <class dataType> 
dataType &Array<dataType>::operator[](int subscript) {
  // check for subscript out of range error
  assert( subscript >= 0 );
  
  if(subscript > capacity)
  {
	dataType *temp = new dataType[subscript];
	for (int i = 0; i < capacity; ++i)
	{
		temp[i]=*this[i];
		temp.capacity = subscript;
		temp.numUsed = subscript;
	}
  }
	else if(subscript > numUsed)
  {
	  numUsed = subscript;
  }
  return ptr[subscript]; // reference return
}



I really have no idea what I'm doing and classmates that I've texted to ask questions have ignored me, so I'm swallowing my embarrassment at posting my poor attempt at coding online hoping that I can get some help. If there's another part of code that needs posted, or more information or just better formatting needed, please let me know.
Last edited on
temp[i] = *this[i]; should be temp[i] = (*this)[i]; or temp[i] = ptr[i];

Also note that indices start from zero so the size (number of elements) is not a valid index. You need to subtract one to get the index of the last element. new dataType[subscript] creates an array of size subscript but that means subscript will not be a valid index into that array. It seems you are making these kind of mistakes throughout the code.
Last edited on
«instantiated from here» is not an error message
your errors are:
- error: no match for ‘operator*’ (...)
- error: request for member ‘capacity’ (...)
- error: request for member ‘numUsed’ (...)
(notice that they start with «error:»)


«[with dataType = int]»
replacing the relevant part of your code
1
2
3
4
5
6
7
	int *temp = new int[subscript];
	for (int i = 0; i < capacity; ++i)
	{
		temp[i]=*this[i];
		temp.capacity = subscript; //¿?
		temp.numUsed = subscript; //¿?
	}
¿see the problem? `temp' is a pointer to int, ¿what's this `capacity' that you talk about?

also, ¿what's the point? you later simply discard `temp'
Topic archived. No new replies allowed.