// I want to make a program having Queue class where message class instances are being used as messages. Later I want to use another class exchange that takes the messages and distribute to the Queue according to messageID.
if message id is "111" the Queue named Q11 should be created and message with ID-111 routed to this queue. Please HELP! am I using the right concept of friend's classes. Suggest please.....

#include "stdafx.h"
#include<iostream>
#include<stdlib.h> 
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Linq;
int someSize=2000;
# define SIZE 20

class message
{
 int MsgId;
 friend class queue;
public:
	message();
	message(int Id);
	int getMessageId();
	viod setMessageId(int id);
};
message::message(int Id)
{
 MsgId=Id;
}
int message::getMessageId()
 {
    return MsgId;
  }
void message::setMessageId(int id)
 {
     MsgId=id;
  }


class queue
{
	message aMsg[SIZE];
	int front;
	int rear;
public:
	queue();
	
	void insertMessage(message i);
	message removeMessage();
	bool isempty();
	bool isfull();
	void Displayitem();
};

queue::queue()
{
front=0;
rear=0;
}

void queue::insertMessage(int i)
{
if(isfull())
{
	 Console::WriteLine("******Queue is FULL !!!No insertion allowed further.******");
	return;
}
aMsg[rear].MsgId = i ;
rear++;
}
int queue::removeMessage()
{
 if(isempty())
  {
	return -1;
  }

  return(aMsg[front++].MsgId);
}
bool queue::isempty()
 {
  return(front == rear);
 }
bool queue::isfull()
 {
 return(rear == SIZE);
 }
//void queue::Displayitem()
//{
//   if(isempty()) 
//	 Console::WriteLine("****Empty*****");
//      else
//		for(int i=front; i!=rear; i++)
//	          Console::WriteLine("the queue is having item {0}", aMsg[i]);
//  }


int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
	queue q;
	
    return 0;

}

Recommended Answers

All 14 Replies

Simply put, a friend class has access to the private and protected parts of the class that made it a friend. In your case, the queue class can access all the parts of a message, and that includes write access. Be sparing with your use of "friends" - they can turn on you if you are not careful! :-) In any case, you are better off if you provide a good set of public methods in the message class that queue can use without making it a friend.

Thank you very much for your reply. How can I make a method public that it could be accessed by other class instances I tried it using "Public" keyword with every class but its giving me errors. Please help

Don't capitalize "public". IE,

class foo : public bar
{
private:
    // Private components here - only accessible by this class and friends.
    int m_int;

public:
    // Public components here - accessible by the world.
    foo() : m_int(0) {}
    foo( const foo& cpy ) : bar(cpy), m_int(0) {}
    virtual ~foo() {}
    foo& operator=(const foo& rhs);

    int getInt() const { return m_int; }
    void setInt( int newValue ) { m_int = newValue; }

protected:
    // Some protected components here. Accessible by self, friends,
    // and derived classes.
};

I couldn't get this I am sorry about that, Where we will define the Bar class definition, Please help

I think I am not explaining very well what actually I am looking for.


I want to make a queue class which is storing message class objects in it.

Class Queue
{

message aMsg;
Public:

// all other methods to manipulate this array of messages that is a queue. Is there any other simple method I can do these object interactions. I have searched a lot of stuff but all comes in term of inheritance which I don't have in my case.

In your example:

Class Queue
{
    message aMsg[SIZE};
Public:
.
.
.
};

should be this:

class Queue
{
    message aMsg[Size];
public:
.
.
.
};

Note the lack of Capitalization on the keywords 'class' and 'public'. You used 'Class' and 'Public', which will generate errors when compiled. Most programming languages (except perhaps BASIC and some others) are very case-sensitive. IE, the term 'Foo' is NOT the same as 'foo'.

In my example, the base class 'bar' is not shown. It can be any class, or even a simple C-type struct. My purpose in using it was to show you how to "friend" another class, and how friends differ (access to all, including private, members) from derived classes (access to public and protected members). I hope this is a bit clearer for you.

Thank you very much I would be careful in case sensitivity, Now I have thought to solve this issue by making Message as a "struct". my program is working well having array of struct message in the queue class.
But still another class exchange got the same problem. which has to use the queue objects

Well, a struct will work because in C++ a struct is a class where all members (variables and methods) are public. Perhaps you can post your headers, and the problem code with any changes you have made here for us to review? I know you posted before, but it would be helpful to see what (if any) changes you have made. Also, you need to tell us if the errors are compile-time errors (post the error messages here), or run-time errors/exceptions. If the latter, try to find where they are being thrown from, and tell us that as well.

the new code is here:

// service.cpp : main project file.

#include "stdafx.h"
#include<iostream>
#include<stdlib.h> 
using namespace System;
using namespace System::Collections::Generic;
using namespace System::Linq;
int someSize=2000;
# define SIZE 20

class exchange
{
  bool Durable;
  int type;
  bool AutoDelete;
  bool Arguments;

public:
    exchange();
    exchange(bool D, bool AD, int T);
    void CreateBinding(message m);
    int getType();
    void setType(int t);
    void DelExchange();
  };

 exchange::exchange()
  {
  Durable= false;
 AutoDelete=false;
  }
 exchange::exchange(bool D, bool AD, int T)
 {
    Durable= D;
    AutoDelete=AD; 
    type=T;
 }
 void exchange:: DelExchange()
 {
   Durable= false;
   AutoDelete=false;
   type=0;
  }

 int exchange::getType()
  {
   return type;
  }


 void exchange::setType(int t)
  {
    type= t;
  }

void exchange::CreateBinding()
{
  for(int i=front; i!=rear; i++)
  {
      if(aMsg[front].MsgId==101)
   {
     queue q101;
     q101.insertMessage(101);
   }
  else if(aMsg[front].MsgId==102)
    {
     queue q102;
     q101.insertMessage(102);
    }
  }

struct message
{
  int MsgId;
  char properties; 
};

class queue
{
    message aMsg[SIZE];
    int front;
    int rear;
public:
    queue();

    void insertMessage(int i);
    int removeMessage();
    bool isempty();
    bool isfull();
    void Displayitem();
};

queue::queue()
{
front=0;
rear=0;
}

void queue::insertMessage(int i)
{
if(isfull())
{
     Console::WriteLine("******Queue is FULL !!!No insertion allowed further.******");
    return;
}
aMsg[rear].MsgId = i ;
rear++;
}
int queue::removeMessage()
{
 if(isempty())
  {
    return -1;
  }

  return(aMsg[front++].MsgId);
}
bool queue::isempty()
 {
  return(front == rear);
 }
bool queue::isfull()
 {
 return(rear == SIZE);
 }
void queue:: Displayitem()
{
   if(isempty()) 
     Console::WriteLine("****Empty*****");
      else
        for(int i=front; i!=rear; i++)
              Console::WriteLine("the queue is having item {0}", aMsg[i].MsgId);
  }


int main(array<System:: String ^> ^args)
{
    Console::WriteLine(L"Hello World");
    queue q;
    q.insertMessage(101);
    q.insertMessage(102);
    q.insertMessage(103);

    q.Displayitem();

    return 0;

}

*******************************************************************************************error messages***********************************************************

1>------ Build started: Project: service, Configuration: Debug Win32 ------
1>  service.cpp
1>service.cpp(22): error C2061: syntax error : identifier 'message'
1>service.cpp(59): error C2065: 'front' : undeclared identifier
1>service.cpp(59): error C2065: 'rear' : undeclared identifier
1>service.cpp(61): error C2065: 'aMsg' : undeclared identifier
1>service.cpp(61): error C2065: 'front' : undeclared identifier
1>service.cpp(61): error C2228: left of '.MsgId' must have class/struct/union
1>service.cpp(63): error C2065: 'queue' : undeclared identifier
1>service.cpp(63): error C2146: syntax error : missing ';' before identifier 'q101'
1>service.cpp(63): error C2065: 'q101' : undeclared identifier
1>service.cpp(64): error C2065: 'q101' : undeclared identifier
1>service.cpp(64): error C2228: left of '.insertMessage' must have class/struct/union
1>          type is ''unknown-type''
1>service.cpp(66): error C2065: 'aMsg' : undeclared identifier
1>service.cpp(66): error C2065: 'front' : undeclared identifier
1>service.cpp(66): error C2228: left of '.MsgId' must have class/struct/union
1>service.cpp(68): error C2065: 'queue' : undeclared identifier
1>service.cpp(68): error C2146: syntax error : missing ';' before identifier 'q102'
1>service.cpp(68): error C2065: 'q102' : undeclared identifier
1>service.cpp(69): error C2065: 'q101' : undeclared identifier
1>service.cpp(69): error C2228: left of '.insertMessage' must have class/struct/union
1>          type is ''unknown-type''
1>service.cpp(85): warning C4822: 'exchange::CreateBinding::queue::queue' : local class member function does not have a body
1>service.cpp(87): warning C4822: 'exchange::CreateBinding::queue::insertMessage' : local class member function does not have a body
1>service.cpp(88): warning C4822: 'exchange::CreateBinding::queue::removeMessage' : local class member function does not have a body
1>service.cpp(89): warning C4822: 'exchange::CreateBinding::queue::isempty' : local class member function does not have a body
1>service.cpp(90): warning C4822: 'exchange::CreateBinding::queue::isfull' : local class member function does not have a body
1>service.cpp(91): warning C4822: 'exchange::CreateBinding::queue:: Displayitem' : local class member function does not have a body
1>service.cpp(95): error C2143: syntax error : missing ';' before '{'
1>service.cpp(96): error C2065: 'front' : undeclared identifier
1>service.cpp(97): error C2065: 'rear' : undeclared identifier
1>service.cpp(101): error C2601: 'exchange::CreateBinding::queue::insertMessage' : local function definitions are illegal
1>          service.cpp(58): this line contains a '{' which has not yet been matched
1>service.cpp(111): error C2601: 'exchange::CreateBinding::queue::removeMessage' : local function definitions are illegal
1>          service.cpp(58): this line contains a '{' which has not yet been matched
1>service.cpp(120): error C2601: 'exchange::CreateBinding::queue::isempty' : local function definitions are illegal
1>          service.cpp(58): this line contains a '{' which has not yet been matched
1>service.cpp(124): error C2601: 'exchange::CreateBinding::queue::isfull' : local function definitions are illegal
1>          service.cpp(58): this line contains a '{' which has not yet been matched
1>service.cpp(128): error C2601: 'exchange::CreateBinding::queue::Displayitem' : local function definitions are illegal
1>          service.cpp(58): this line contains a '{' which has not yet been matched
1>service.cpp(138): error C2601: 'main' : local function definitions are illegal
1>          service.cpp(58): this line contains a '{' which has not yet been matched
1>service.cpp(151): fatal error C1075: end of file found before the left brace '{' at 'service.cpp(58)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Hi,

Actually I want to have simple thing that queue and message both classes and their functions are accessible via exchange class as
createBinding method will check the messages coming in the first queue q and distribute to the new queues, that it will create according to types of the message.

please help me

commented: Be patient. People don't live here. They come and go. -4

It may be that the keyword "message" conflicts with something else. Try replacing "message" with something like MyMessage instead and see if that works.

No I dont think so...as without exchange class it was working very well. as I already written that I have made message struct.

Actually its not allowing to use me queue class objects and methods in exchange class. I cannot define a new object of queue in exchange's method which I intend to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.