Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2004, 9:24 AM   #1
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
Hi all!

I was working with classes, and was trying to test the friend directive. In the code below I keep getting the following error on compilation:

c:\c__~1\new\prac\struct~1\friend.cpp:35: no `int stack::compare(stack, stack)' member function declared in class `stack'

The code is below. I hope someone can help me out?

Cheers

#include <iostream.h>

const int SIZE = 100;

class stack {
	private:
 int count;
 int data[SIZE];

	public:
 void init( void );
 void push( const int item );
 int pop( void );
 friend int compare( const stack &stack1, const stack &stack2 );
};

void stack::init( void )
{
count = 0;
};

void stack::push( const int item )
{
data[count] = item;
++count;
};

int stack::pop( void )
{
--count;
return(data[count]);
};

int stack::compare( const stack stack1, const stack stack2 )
{
if( stack1.count != stack2.count )
return(0);
	for( int i = 0; i < stack1.count; ++i )
	{
	if( stack1.data[i] != stack2.data[i] )
	return(0);
	};
return(1);
};

main()
{
class stack myStack1;
class stack myStack2;

myStack1.push(4);
myStack2.push(3);

cout << compare( myStack1, myStack2 ) << '\n';

return(0);
};
sys0p is offline   Reply With Quote
Old Nov 25th, 2004, 11:06 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
You have two problems:

>int stack::compare
compare is a friend, not a member. You don't qualify friends as if they were members unless they actually are.

>friend int compare( const stack &stack1, const stack &stack2 );
>int compare( const stack stack1, const stack stack2 )
Notice how the declaration takes references to a const stack and the definition takes const stack objects. These are two different functions and the compiler will treat them as overloads. Change the definition to match the declaration and the code will compile:
class stack {
private:
 int count;
 int data[SIZE];

public:
 void init( void );
 void push( const int item );
 int pop( void );
 friend int compare( const stack &stack1, const stack &stack2 );
};

...

int compare( const stack &stack1, const stack &stack2 )
{
 if( stack1.count != stack2.count )
  return(0);
 for( int i = 0; i < stack1.count; ++i )
 {
  if( stack1.data[i] != stack2.data[i] )
   return(0);
 };
 return(1);
};
Eggbert is offline   Reply With Quote
Old Nov 25th, 2004, 1:39 PM   #3
sys0p
Programmer
 
Join Date: Oct 2004
Posts: 73
Rep Power: 5 sys0p is on a distinguished road
Hey!
Oh, Oh, Oh what a stupid mistake to make!!! Thanks for pointing out where I went wrong. At least, know I know where I went wrong
Cheers
sys0p is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC