Someone came into #C++ this evening with what turned out to be a very simple compiler error.

In header.c:

  1. MyObjCluster::MyObjCluster(  
  2. const std::vector<MyObj> <em>a,  
  3. std::vector<MyObjCluster>& b) { /</em> ... */ };  

In main.c:

  1. std::vector<MyObj> <em>A = new std::vector<MyObj>();  
  2. std::vector<MyObjCluster> B;  
  3. MyObjCluster </em>testfinder = new MyObjCluster(A,&B);  

The error? "Conversion from std::vector<MyObjCluster, std::allocator<MyObjCluster> >* to non-scalar type std::vector<MyObjCluster, std::allocator<MyObjCluster> > requested."

And C++'s confusing type system strikes once again.

The guy was confused, thinking that you need the '&' in both the function signature and the calling line to pass by reference, and in this case the solution is of course to simply omit the '&' before B in main.c.

"Messy type system?", you ask dubiously. Here's a typedef that will have the token myFunctionPointer represent the function void* myFunction(int a, char* b):

  1. typedef void<em> (</em>myFunctionPointer)(intchar *);  

Yes, it's a mess. Add to that the fact that the asterisk has two — largely distinct — meanings (or three if you count multiplication on top of pointer-type and dereferencing) as does ampersand for just the same reason, which is what started this particular topic off.

Discuss.