-1

I have a function in header file Class2.hpp of the form:

class Class2{
void Test::func(std:: string & text, Class1::enum_var var1, unsigned var2, unsigned     &var3);};

I have Class1 of the form:

  class Class1{
  public:
 /// Different literal types
  enum enum_var {
  enum_var1, enum_var2, enum_var3
 }; };

I have defined the above function in Class2.cpp as

 void Test::func(std:: string & text, Class1::enum_var var1, unsigned var2, unsigned     &var3){
 cout<<"inside the function";
 }

Now I am calling the above function as:

Class2 clsObj;
std::string text="abc";
int var3=0;
clsObj.func( text, Class1::enum_var1, 0, &var3);

However, when I am running the program its giving me the following errors:

 error: no matching function for call to ‘Class2::func(std::string*,   Class1::enum_var1,  int, int*)’
 note: candidate is:
 void Class2::func(std::string&, Type::ID, unsigned int, unsigned int&)
 note:  no known conversion for argument 4 from ‘unsigned int*’ to ‘unsigned int&’

I am not getting how to get rid of the error..can someone be kind enough to help PLEASE.

Also, I want the changes in var3 to be reflected back...therefore apart from returning or passing by reference I have no choice..and when I pass var3 by reference its giving me errors.

user1355603
  • 305
  • 2
  • 11

2 Answers2

2

Passing by reference has no special syntax on the calling side of things, only the signature/definition has the special &. Use:

clsObj.func(text, Class1::enum_var1, 0, var3);

&text gets the address of the text variable (which is of type std::string*). When you try to pass that to the function, it isn't expecting a pointer, and that's exactly what the error message is trying to say.

Cameron
  • 96,106
  • 25
  • 196
  • 225
  • @Als: Oop, that's what I get for copying the OP's code without looking too carefully. There was a comma missing between the third and fourth. Good catch! – Cameron Jun 07 '12 at 03:21
  • @Cameron I want the changes in var3 to be reflected back...therefore apart from returning or passing by reference I have no choice..and when I pass var3 by reference its giving me errors. – user1355603 Jun 07 '12 at 03:23
  • @user: This *is* passing by reference. `var3` is passed by reference because the argument type in the function definition is declared as `unsigned int&` (reference to unsigned int). `&var3` is something else entirely (address-of resulting in a pointer). You could also pass pointers around, sure (then you'd have to change the, but why do that when you can pass by reference? – Cameron Jun 07 '12 at 03:25
  • Oops, I left a sentence trailing. The last part of my previous comment should read "You could also pass pointers around, sure (then you'd have to change the function signature to accept a pointer and the body to use the pointer), but why do that when you can pass by reference?" – Cameron Jun 07 '12 at 03:35
2
clsObj.func(& text, Class1::enum_var1, 0 &var3);

should be:

clsObj.func(text, Class1::enum_var1, 0, var3);

Reference is an alias to the object being referred and the refferant is accessible just by the reference name.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • but I want the changes in var3 to be reflected back...therefore apart from returning or passing by reference I have no choice..and when I pass var3 by reference its giving me errors. #DONT KNOW HOW TO CORRECT IT – user1355603 Jun 07 '12 at 03:21
  • @user: "It's giving you errors" -- what errors? And, this *is* passing by reference. – Cameron Jun 07 '12 at 03:22
  • 1
    @user1355603: Your function declaration passes `var3` by reference, the calling code merely needs to pass `var3` and not `&var3`, your function should be still able to modify `var3` and those changes will be reflected outside the function.As for the errors you are getting the answer already says whats wrong. – Alok Save Jun 07 '12 at 03:24
  • @Cameron "var3" is not passed by reference..its an integer....hence its NOT PASSED BY REFERENCE WITH "clsObj.func(text, Class1::enum_var1, 0, var3); " – user1355603 Jun 07 '12 at 03:25
  • @user: I think you should get a C++ reference book. You clearly don't understand the syntax. I'm trying to tell you how pass-by-reference works; denying what I'm saying won't make it any less true. `var3` is a variable in memory (more accurately, an lvalue) which can therefore be passed by reference like any other variable. – Cameron Jun 07 '12 at 03:29
  • @Als Thanks for replying. I tried to check the value...of var3...its remains the same..i.e. changes are not getting reflected back....its an integer and when you pass an integer as "var3" instead of "&var3" it gets passed by value and not by reference...hence the changes are NOT getting reflected for var3 – user1355603 Jun 07 '12 at 03:29
  • 2
    @user1355603: [CLEARLY INCORRECT!!](http://ideone.com/qFr57).Whoever taught you that or whatever presumption you have is incorrect. And yes I do agree to Cameron's suggestion that you need a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Alok Save Jun 07 '12 at 03:29