Hi guys,

I have been away from programming for a while and i started back today now that i have some time again. However i have run into a bit of a brick wall. im trying to impliment a queue with a simplified interface for holding messages.

What I have got:

//class msgQ
class msgQ
{
public:
   msgQ()  : m_numMSGs(0) {};
   ~msgQ() { m_MSGs.clear(); };

   void GetMSG( msg& newMSG );
   void AddMSG( msg& newMSG );
   int GetNumMSGs( void ) { return m_numMSGs; };
   
protected:
   int m_numMSGs;
   std::list<msg> m_MSGs;
};

//add msg 
void msgQ::AddMSG( msg& newMSG )
{
   m_MSGs.push_back( newMSG );
   ++m_numMSGs;
}

// in main()
msg myNewMessage;
msgQ myQ;

cout << "adding element!" << endl;
myQ.AddMSG(&myNewMessage); // <---- this line is where it says the error is
cout << "Q size: " << myQ.GetNumMSGs() <<endl;

To this visual studio says : cannot convert parameter 1 from 'msg *' to 'msg &'

From what i understood when passing things to a function, it works like as i lay out below

//if i make classType classInstance;
someFN( classInstance );  //function gets a copy of classInstance
someFN( &classInstance ); //function gets reference to classInstance
//and then if i had done classType *classInstance;
someFN( classInstance );  //function gets the pointer classInstance which points to an actual instance

So like i said then, from what i understand as i have asked for a reference in my function and im passing a reference it should not be expecting a pointer?

I aplogise if this seems like a stupid question but i really dont understand the problem it is finding.

Thanks in advance

Try changing line 29 to

myQ.AddMSG(myNewMessage);

Thanks ,... that works perfect but i dont understand why it works? Are class instances like array names in that the name itself is enough to be used as a reference/pointer? ,...

If you could tell me why it works id greatly appreciate it as i dont like not understanding those subtle things in c++

Again thanks :)

Your example on line 3

someFN( &classInstance );

This passes the address of classInstance not the reference of classInstance.

Yes i agree with that it passes the address but as i udnerstood that in passing the address using & eliminated the need for a pointer, so it was called a reference ,... so what i think i am really asking then is how does passing the instance to my function pass it by address without using & to pass the address. so would i be right in assuming then that like an array if i supply the name with no & the compiler uses the address of the instance and does not make a copy of it as the function prototype asks for an address?

I just started back into C++ from taking some time off from it as well, slight rusty, but let's see if I can give you a shot at what you are asking for.

class FullOfFunctions{
public:

void function(int happy_num); //pass by value
void function2(int & happy_num); //pass by reference
private:
};

When you use the function for either in the main() program, their use will look the same.

int main() {

int happy_num, unhappy_num;

unhappy_num = function(happy_num); // pass by value
unhappy_num = function2(happy_num); // pass by reference

return 0;
}

void FullOfFunctions::function(int happy_num) {
//purposely left blank
// random code
}
void FullOfFunctions::function2(int & happy_num){
// purposely left blank
// random code
}

As far as I'm aware, even with input/output streams to class member functions, you will not need the & while calling the function in the main() program. Don't quote me on this but the compiler will know prior to manifesting the program if the variable passed, including passing objects of classes or structures, will be either "pass by variable or reference."

I hope this helps.

I feel like a goof, trying to help out but upload incorrect programming on my very first entry! ><

CORRECTION:
function(happy_num); // pass by value
function2(happy_num); // pass by reference

Incorrect:
unhappy_num = function(happy_num); // pass by value
unhappy_num = function2(happy_num); // pass by reference

** voids can not return int **

Thanks saith,. that makes sence. Much appreciated and i guess that makes this case closed :)

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.