friend function question

W

wongjoekmeu

Hello all,
I have the following problem, maybe someone can help me out.
I have a template class A which has a protected member Aint.
Now I have template class B which has as private member an
instance of A. Now to get the value of Aint I decided to create
a friend function and declare them in both classes. But I cant
get this program to compile as it is complaining about an undeclare
reference. First I was wonderin if with template classes this trick
can be performed with friends function to get access to the protected
member and if anyone could tell me what I am doing wrong.
Thanks in advance.
Robert

int main()
{
GetAint() ;
}

template <class itemType>
class B;

template <class itemType>
class A
{
friend int GetAInt( A<itemType>&);
protected:
int AInt;
}

template <class itemType>
class B

{
friend int GetAInt( A<itemType> & );

private:
A<itemType> myA ;
}

template<class itemType>
int GetAInt(A<itemType> &myA)
{
...
}
 
V

Victor Bazarov

Hello all,
I have the following problem, maybe someone can help me out.
I have a template class A which has a protected member Aint.
Now I have template class B which has as private member an
instance of A. Now to get the value of Aint I decided to create
a friend function and declare them in both classes. But I cant
get this program to compile as it is complaining about an undeclare
reference. First I was wonderin if with template classes this trick
can be performed with friends function to get access to the protected
member and if anyone could tell me what I am doing wrong.
Thanks in advance.
Robert

int main()
{
GetAint() ;

'GetAint' is undefined. Perhaps you should move 'main' function to the
end of your program.
}

template <class itemType>
class B;

template <class itemType>
class A
{
friend int GetAInt( A<itemType>&);

Since GetAInt accepts an argument dependent on the type, perhaps you
should declare it before the class, just like you did with class B.
protected:
int AInt;
}

template <class itemType>
class B

{
friend int GetAInt( A<itemType> & );

private:
A<itemType> myA ;
}

template<class itemType>
int GetAInt(A<itemType> &myA)
{
...
}

Where do you use your function? Which line of your program does the
compiler complain about?

Read FAQ 5.8.

V
 
H

Howard

'GetAint' is undefined. Perhaps you should move 'main' function to the
end of your program.

In case you're wondering why it's undefined, there are three problems:

1) You define the function below as "GetAInt", not "GetAint". C++ is
case-sensitive.
2) The GetAInt function takes a parameter of type A<itemType>&. Yo haven't
supplied any parameter.
3) You're trying to use a function that is defined later in the code. You
can't use a function that hasn't been declared yet. (Usual practice is to
always put main() at the bottom.)

Also, what does B have to do with it? You're not even creating an instance
of a B object anywhere. Given your design, my *guess* is that you wanted to
declare a B object, then get the int from its myA member. To do that, write
a function in B that passes its myA member to the function GetAInt.
Since GetAInt accepts an argument dependent on the type, perhaps you
should declare it before the class, just like you did with class B.

That function doesn't need to be a friend of class B. If you want a
function in B to call GetAInt, just call it, passing the member variable myA
as the parameter. (So write a member function of B that does that.)

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top