-2

I have to put overloaded constructor form class Parameters into a function in class Solver. Here is Parameters Header:

#ifndef Parameters
#define Parameters

#include <iostream>
#include<conio.h>
#include<fstream>
#include<string>
using namespace std;

class Parameters
{
    int M;
    double dx;
    double eps;
public:
    Parameters( );
    Parameters(int M1, double dx1, double    eps1 );
    Parameters( string  fileName);
};
#endif

The constructor initializes M, dx, eps with default values or chosen by the user from the keyboard or from the file.

I want to have another class which will be containing this initialized values (in order to solve some equation lately, also in this class).

The problem is that although I tried to do this by value, reference or/ pointers, there was always some error or code compiled but done nothing.

Here's my Solver class:

#include "ParametersH.h"
#include "EquationH.h"
#include <iostream>
#include<conio.h>
#include<fstream>
#include<string>
#include<vector>

using namespace std;
class Solver
{
public:
    int Solve( Parameters& obj );
};

int Solver::Solve( Parameters& obj)
{
    cout << obj.M; // M is private so it fails :<
    // another attempt was like this:
    Parameters *pointer = new Parameters();
}

int main()
{
    Solver Solve();
    return( 0 );
}

I really couldn't handle this, hope someone will help.

JeJo
  • 30,635
  • 6
  • 49
  • 88
Uroboros
  • 39
  • 5
  • `Solver Solve();` this declares a function named `Solve()` without parameters that returns a `Solver` object. You might want to change it to `Solver solver; solver.Solve(existingParameterObject);`. – lubgr Aug 21 '18 at 09:35
  • @lubgr but Parameter class object is an overloaded constructor only... Would you explain more widely what should I put there? – Uroboros Aug 21 '18 at 09:44
  • 1
    `Solver::Solve(Parameters& obj)` does not take a constructor as its argument, but an object of type `Parameters`. Hence, you have to construct it first. – lubgr Aug 21 '18 at 09:48
  • @lubgr here: Solver::Solve(Parameters& obj) Parameters stands for name of the class, am I right? By type of the object of class Parameters you mean types of the M, dx, eps, since constructor Parameters fills them? So i have to construct object in class Solver of type such as M, dx, eps? – Uroboros Aug 21 '18 at 10:00
  • @JeJo i tried to do this by friend classes, failed. error was "request for member M (of private variable from class Parameters) in 'obj' which is of non- class type 'int'. – Uroboros Aug 21 '18 at 14:02

1 Answers1

1

As @lubgr mentioned in the comments, here

Solver Solve();

you declare a function named Solve() without parameters that give you a Solver object, not the member function Solve( Parameters& obj );

If your goal is to access private members of class Parameters in class Solver: You can either

  • define Solver a friend of Parameters: See here or
  • access through setters and getters or
  • make a struct of Parameters by which you can access everything inside it

However, it looks like you only need a struct Parameters and a simple function Solve( Parameters& obj);, which will do your job.

struct Parameters
{
    int m_;
    double dx_, eps_;
    Parameters(int M1, double dx1, double   eps)
        : m_(M1), dx_(dx1), eps_(eps) // provide other contrs as per
        {}
};

int Solve(Parameters& obj)
{
    std::cout << obj.m_ << " " << obj.dx_ << " " << obj.eps_ << std::endl;
    return obj.m_;
}

now in the main() simply:

std::cout << "result: " << Solve(Parameters(1, 2.0, 3.0));
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • errors are: "request for member 'm_, eps_ dx_' which is of non-class type 'int' " and "Solve was not declared in this scope" (why since its declaration is in the class) – Uroboros Aug 21 '18 at 15:02
  • 1
    @Uroboros If you are following the first option I mentioned: In order to access the members of `Parameters` class(i.e, `m_, eps_ dx_`) you need to pass a objecto `Parameters` to the function all the time. Like `Solve( Parameters& obj );` in the example : https://ideone.com/hCbJYa code – JeJo Aug 21 '18 at 15:05
  • 1
    (Thank you for patience) I tried with struct and friend, same errors. Corrrect me of I'm wrong: variables m_, eps_,dx_ are for 'storing' variables of class Parameters: M, eps, dx? – Uroboros Aug 21 '18 at 15:27
  • here for struct: https://ideone.com/KA1Kz1 , and for friend solution I simply added 'fried class Solver' after M,eps,dx declaration in Parameters and Solver class: https://ideone.com/m0T5zO . I puted struct here, because I was confused with the fact that in your example there was modification of parameters of my existing class Parameters... or should it be exactly like that? – Uroboros Aug 21 '18 at 15:58
  • 1
    @Uroboros Now I see. You have `Parameters` inside the private section of `Solver` which is only accessible by `Solver`, not by the outside world. That's where the problem comes from :) Note that in order to creat a object of `Parameters` class you need it outside the `Solver` class. Like this: https://www.ideone.com/Axa5gO The way you thingking is not a good Change like in the link and try it. – JeJo Aug 21 '18 at 16:39
  • When I make changes in Parameter class Header, just like you mentioned, + put class Solver just like in your code, there are the same mistakes, still ;( – Uroboros Aug 21 '18 at 21:41
  • 1
    *"Solve was not declared in this scope" * from thins error I think, you need to include `Solver.h` in `ParametersH.h` so that linker can look for `Solver` in that. Remeber, we have this line `friend class Solver ;` in `ParametersH.h`. – JeJo Aug 21 '18 at 21:52
  • Actually yes. I've done it by using setters and getters, because what you suggested was working only with classes in one file- you were probably right about my problems with class management. After all thank you for help, managing that problem was a little step forward for my c++ beginners path :> @JeJo – Uroboros Aug 24 '18 at 13:12