By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

friend function question

P: n/a
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)
{
...
}

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
wo*********@yahoo.com wrote:
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
Jul 23 '05 #2

P: n/a

int main()
{
GetAint() ;
'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.
}

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> & );
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.)

private:
A<itemType> myA ;
}

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


-Howard
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.