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
   msgQ()  : m_numMSGs(0) {};
   ~msgQ() { m_MSGs.clear(); };

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

//add msg 
void msgQ::AddMSG( msg& newMSG )
   m_MSGs.push_back( newMSG );

// 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

6 Years
Discussion Span
Last Post by Kanoisa
Featured Replies

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{

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

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.

Edited by Saith: n/a


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

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

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 :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.