473,320 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

friend function question

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
2 1662
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
6
by: Alexander Grigoriev | last post by:
I've got a problem with GNU C++ for ARM, which is used in WindRiver Tornado 2.11 environment. I tried the following construct: void foo(); namespace N {
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
2
by: vermarajeev | last post by:
Hi, I have a simple question to ask. I was just trying some sample program using friend function. The program goes like this #include<iostream> using namespace std; class B;
4
by: ciccio | last post by:
Dear all, once again I stumbled upon the following puzzling problem. When having the following two files (see below), the gnu compiler compiles the file without a problem while the compiler...
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.