0

I wrote the friend function prototype in a class of which protected members I want to access. It looks like this:

friend void incrementByConstant(arrayListType <elemType>, int a);

protected:
  elemType * list; //array to hold the list elements
  int length; //to store the length of the list
  int maxSize; //to store the maximum size of the list 

Then in the function definition, I tried to access the protected members but I received an error that says that these members are undeclared. Which I understand the compiler doesn't think I am referring to the members in a class, but rather different variables. I don't know how to fix this, as I am still new with practically trying out friend functions. below is my function definition:

template < class elemType >
void incrementByConstant(arrayListType <elemType> &L, int a)
{
   for(int i = 0; i <length; i++)
{
    list[i] += a;
   } }

The errors: "Use of undeclared identifier 'length'" , "Use of undeclared identifier 'list'"

Ziyad
  • 11
  • 4
  • 3
    You still need an .. instance.. did you mean `L.list`/`L.length`? – Jarod42 Feb 26 '21 at 14:27
  • yes, I will add that now – Ziyad Feb 26 '21 at 14:36
  • I got this error instead: Undefined symbols for architecture x86_64: "incrementByConstant(arrayListType&, int)", referenced from: _main in nonmemberArrayList.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) – Ziyad Feb 26 '21 at 14:38
  • `friend` is not template, whereas you probably define a template function (not visible from instantiation). – Jarod42 Feb 26 '21 at 14:45
  • You might look at [declare-template-friend-function-of-template-class](https://stackoverflow.com/questions/18792565/declare-template-friend-function-of-template-class) – Jarod42 Feb 26 '21 at 14:46
  • the template was defined before the definition of the whole class – Ziyad Feb 26 '21 at 17:04

1 Answers1

0

I managed to fix the issue by adding "template < class whatever >" before the friend function prototype.

Ziyad
  • 11
  • 4