Problem passing classes by reference

I have recently moved from java to c++, so I'm not quite experienced with the use of pointers. I'm trying to translate a few of my old programs, and I have this problem:

I make a class (it stands for an ARMA time series), and I have a method wich modifies some of its variables. In other part of my program I have a function wich receives one object of this class as a parameter and, at some point, it calls the method of the ARMA class to modify it; here is my deal I want to pass the ARMA class by reference to this function, because I want the variables of the instance I'm passing to be modified, not those of a copy the method uses. Also, I would like not to declare the function inside the class ARMA, cause I use it in other places too (it's basically a Nelder-Mead optimization what it performs).

Here is a code wich sketches what I've been trying, and exactly the error message I get is "modifyParameter has not been declared".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>

using namespace std;

class ARMA{
      private:
              double parameter;
              
      public:
             void modifyParameter();
             void printParameter();
             ARMA(double);
      };
      
void functionWichModifies(ARMA* arma);   //For what I understood about pointers, here it says
                                         //"This function will receive the direction of an
                                         //ARMA object"
int main(){
       
       ARMA myArma = ARMA(5);
       
       myArma.printParameter();
       
       functionWichModifies(&myArma);    //So I pass the direction as an argument;
                                         //So far, so good; I tried this exact code with an empty bodied
       system("Pause");                  //functionWichModifies (i.e. it does nothing) and no
                                         //error appeared when compiling.
       return 1;
       }
       
ARMA::ARMA(double p){parameter = p;}

void ARMA::printParameter(){ cout << parameter << "\n";}

void ARMA::modifyParameter(){
     parameter +=5;
     }

void functionWichModifies(ARMA* arma){
     
     *arma.modifyParameter(5);       //Here is my problem: when I try the function to actually do something, I'm not quite sure
                                     //about how doing It, since I'm trying to modify the original "myArma" object, I tried this
     }                               //line, which I supossed it said "use the function modifyParameter in the object
                                     //allocated in the direction *arma. 

Thanks in advance, and I'm sorry if my questions turns up to be something really basic; I've read the documentation and still couldn`t figure out what's happening. Greets.
Last edited on
Why are you passing the class by pointer instead of by reference?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void functionWichModifies(ARMA& arma);

void functionWichModifies(ARMA& arma){
     
     arma.modifyParameter(5);
}

int main(){
       
       ARMA myArma = ARMA(5);
       
       myArma.printParameter();
       
       functionWichModifies(myArma); 


If you do pass by pointer then you need to use pointer access for the class.

arma->modifyParameter(5);


The correct way of doing it is (*arma).modifyParameter(5); or arma->modifyParameter(5);

It still won't work because you defined ARMA::modifyParameter() as taking no arguments, so your compiler will complain you're trying to pass one.
1
2
3
void PassByValue(MyClass obj); //how everything is passed in Java
void PassByPointer(MyClass *obj); //similar to Java references
void PassByReferece(MyClass &obj); //not in Java 
Fun fact: In Java, references are passed by value, because references are a primitive type, and all primitive types are passed by value. (It's also why you can't reference other references - you can't reference primitive types)
Last edited on
Thanks a lot; with this I got it solved. I didn't catch very well the difference between passing by reference or passing by pointer, thought they were the same. Not quite familiar now, but at least know what to search about ;).

toum: you're right, the 5 passed is just a typo :(.
Again, thanks a lot.
*arma.modifyParameter(5);
here you are doing *(arma.modifyParameter(5)); and since modifyParameter() does not return pointer, thus you get error.
Topic archived. No new replies allowed.