473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<itemT ype> &myA)
{
...
}

Jul 23 '05 #1
2 1678
wo*********@yah oo.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<itemT ype> &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<itemT ype> &myA)
{
...
}


-Howard
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
17861
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. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
6
4251
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
4439
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 'aFunc(A<char> const&)' ) in the following sample program. Tried it on GCC and Borland's compiler. Can somebody please show me the correct way to fix this? Thanks, Dmitry
3
2083
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. ============================================================== Question 1: ============================================================== Compile the following codes, and run it.
0
1692
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > > ============================================================== > Your code refuses to compile. In function 'foo<a>' 't' is a pointer to > const. Friendship has nothing to do with it. When I remove the attempt > to change 't->c' in foo<a>, it compiles and runs and displays > > T<a, 3>
5
2627
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> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
15
6578
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
2
6248
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
2291
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 complains about the fact that the function foo (which is declared as a template) is not a template! The problem itself vanishes when changing the name of the function foo in the class bar into some other function name, lets say goo. The problem...
21
4699
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 that gets the same result) works as expected in visualc++. I know that this is probably not the right behavior for a compiler but it's the kind of behavior I'm searching for so I was hoping there was a way to do the same thing in gcc. As you know...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.