errors with functions that pass pointers...

0 Raymond Gutierrez · October 4, 2014

   void fixEverflo(double *exp)
   {
        cout << "EverFlo repair initiated...\n";
      cout << "Repair finished!\n" << endl;

        exp = (exp + 5.4); // this is the line in question giving me error
   }

|26|error: invalid operands of types 'double*' and 'double' to binary 'operator+'|

Any ideas how I can change this to accept a pointer variable and run a simple addition ??

PLEASE AND THANK YOU FOR THE HELP :happy:

Post a Reply

Replies

Oldest  Newest  Rating
+1 Horacio Accipiter · October 5, 2014
Yes.
+1 Horacio Accipiter · October 4, 2014
Bro, you need to dereference the pointer to get the variable it points to.


void fixEverflo(double *exp)
{
cout << "EverFlo repair initiated...\n";
cout << "Repair finished!\n" << endl;

*exp = (*exp + 5.4);
}

Just like that ;)

Even better, you can accept a reference!


void fixEverflo(double &exp)
{
cout << "EverFlo repair initiated...\n";
cout << "Repair finished!\n" << endl;

exp = (exp + 5.4);
}


WHOA! :O
0 Raymond Gutierrez · October 5, 2014
Hahaha! Well I'll be, that seems so obvious now that I have seen it written out.

Thank you everyone that got me up and running.

I guess I should ask while I'm here, is accepting a reference the same as passing a pointer into a function?

As in it will change the value of my main() variables even if I make the function outside of main() ?
0 first last · October 4, 2014
*exp = (*exp + 5.4); // the value exp points to is assigned the value exp points to + 5.4


*exp += 5.4; // this might also work 
-2 Franz Schmidt · October 4, 2014
I'm not a pro at this but I hope this is what you want:

#include <iostream>
using namespace std;

void fixEverflo(double &exp)
{
cout << exp << endl;
cout << "EverFlo repair initiated...\n";
cout << "Repair finished!" << endl;
exp += 5.4;
cout << exp << endl << endl;
}

int main(){
double test = 100.5;
fixEverflo( test );
fixEverflo( test );

system("Pause");
}
  • 1

C++

103,740 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator