No matching function

Hello everyone,
I've defined two functions in a header and when I want to use them in my main, xcode recognize them, but when I initialize the arguments, xcode find an error and say No matching function for call to 'compute_yf'. (I've included the header in my main)

I'm turning mad so please help me haha.
Ps : sorry for my english, not my mother tongue.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>


using namespace std;

void compute_yn(double (*f)(int, double, const vector<double> &), double x0,
                double xf, const vector<double> &y0, int p, int N, vector<double> &x,
                vector<double> &y)
{
    double h=(xf-x0)/N;
    vector<double> yk(p);
    for(int i=0;i<N+1;i++)
    {
        x[i]=x0+h*i;//intialise les xi
    }
    for(int i=0;i<p;i++)
    {
        y[i*(N+1)]=y0[i];//on met les conditions initiales dans le vecteur final
    }
    vector<double> k1(p);
    vector<double> k2(p);
    vector<double> k3(p);
    vector<double> k4(p);
    
    for(int a=0;a<N+1;a++) // donne le y(i) pour chaque systeme
    {
        for(int i=0;i<p;i++)
            yk[i]=y[((N+1)*i)+a];//pour pouvoir calucler les  k(i), nous avons besoin de tous les y(i)
        
        for(int i=0;i<p;i++)
            k1[i]=h*f(i,x[a],yk);
        
        for(int i=0;i<p;i++)
            yk[i]=y[(N+1)*i+a]+k1[i]/2.;
        
        for(int i=0;i<p;i++)
            k2[i]=h*f(i,x[a]+h/2.,yk);
        
        for(int i=0;i<p;i++)
            yk[i]=y[((N+1)*i)+a]+k2[i]/2.;
        
        for(int i=0;i<p;i++)
            k3[i]=h*f(i,x[a]+h/2.,yk);
        
        for(int i=0;i<p;i++)
            yk[i]=y[(N+1)*i+a]+k3[i];
        
        for(int i=0;i<p;i++)
            k2[i]=h*f(i,x[a]+h,yk);
        
        for(int i=0;i<p;i++)
        {
            y[(a+1)+i*(N+1)]=y[a+(N+1)*i]+(1./6.)*k1[i]+(1./3.)*k2[i]+(1./3.)*k3[i]+(1./6.)*k4[i];
        }
        
    }
    
}

double expon(int i, double x, vector <double> y)
{
    return y[0];
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, const char * argv[]) {
    // insert code here...
    double x0=0.;
    double xf=1.;
    int p=1;
    int N=2;
    vector<double> y_o(p,0);
    vector <double> y_f(p,0);
    vector <double> x(N,0);
    y_o[0]=1;
    double eps=0.0001;
    
    compute_yf(expon, x0, xf, y_o, p, eps, N, y_f);
    
    
    
    return 0;
}


Last edited on
Hello Emilien,

Before you start posting code:


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



(I've included the header in my main)

But why not here?

Without any code there is no idea what you have done or done wrong. Also there is no way to try to duplicate the error you are getting.

Posting the whole code that can be compiled is helpful, but at least the header file that you speak of and it would not hurt to include the first lines of "main", the include files.

Andy
Change that to this and this to that and bob's your uncle.
Lmao dutch, is your your uncle 43 btw? :)
is your your uncle 43 btw?

I don't get that. ???
Oh crap I somehow managed to mess it up! I meant to say 42 ...
Now I'm even more confused ....
Haha well it's a recurring meme on this forum.

If you want to have a look at the reference
https://www.youtube.com/watch?v=sgeMrzbfrxg
https://www.dictionary.com/e/slang/42/
Last edited on
Oh, THAT 42. I see. :-)
;)
@H00G0,

Thank you I have not seen the second link before.

Should have said that earlier.

Andy
You're welcome Handy Andy, my pleasure :)
Hey, I've edited the post, I put the function in the main, so the error would not be coming from the header inclusion. So maybe in my function utilisation ?
Last edited on
> void compute_yn(double (*f)(int, double, const vector<double> &),
Here, you say f takes a const reference to a vector.

> double expon(int i, double x, vector <double> y)
But this is a non-const by value.

Make them agree with one another.
I've tried and still not working;
Hello Emilien,

It took a while, but I finally understood what salem c is saying. Although I see that as only part of the bigger problem.

No matching function for call to 'compute_yf'

This should have been the first indication of the problem.

1
2
void compute_yn( // <--- Function definition.
     compute_yf( // <--- Function call. 

See a problem. Check your spelling. There is no function compute_yf.

Then there is the parameters for the function
1
2
3
(double(*f)(int, double, const vector<double> &), double x0,
double xf, const vector<double> &y0, int p, int N, vector<double> &x,
vector<double> &y)

This first part (double(*f)(int, double, const vector<double> &), looks to me to be a prototype. Not sure if it would even work like this, and it is not where I would put it, but I would refer to what salem c said.


Then in the function call compute_yf(expon, x0, xf, y_o, p, eps, N, y_f);
                                   ^

The first parameter looks to be a call to the function "expon", but it is missing its parameters. Since the function is defined as double expon(int i, double x, vector <double> y) When I get to the third parameter I have no idea what should have been sent to the the function because "y" has no meaning to me. Maybe it does for you.

While talking about variable names please use something other than a single letter. The single letters make the program hard to understand and follow because someone other than you is always trying to figure what a single letter variable is for. This is more for your benefit in the future.

Your variable names of "x0", "xf", "p", "N" and the "y_"s and "x" may mean something to you, but for the rest of us thy are hard to follow. Using a good variable name in "main" can also be used in a function because it looses scope in main when the function is called and becomes a local variable in the function. This makes no difference if the variable is defined as a function parameter or between the {}s of the function.

As an example if you wrote the function definition as: double expon(const vector<double>& y_o). It is easy to understand which vector is being used. Also as for now the first two parameters are not used. Passing the vector as a const reference means that you are not taking the time to make a copy of the vector and the "const" means that you can not change the vector.

In the function call compute_yf(expon, x0, xf, y_o, p, eps, N, y_f);. Since "expon" is a function that you are trying to call to use its return value for the "compute" function call "expon" would be called first and then its return value would be used a a parameter of the "compute" function call. But you would have to make it a true function call to use it. Or you could use compute_yf(y_o[0], x0, xf, y_o, p, eps, N, y_f); and do the same thing as the function just returns a value that can just as easily be done here. I used "y_o" as an example because I am not sure what vector yo want to use.

To jump back to something you first said:
I've defined two functions in a header

As I understand this these functions should be in a ".cpp" file not a header file. The header file should contain the prototypes of the functions. The only exception I can think of is when you use a template on the prototypes and the function definition. These work best when they are in the same file.

Sorry to jump around, but int main(int argc, const char * argv[]). These parameters are not used in the program and do not need to be here unless yo have some use that you have not coded for yet. int main() is all you need.

There are still some things I need to test and check. If I find something else I will let you know.

Hope that helps,

Andy
Topic archived. No new replies allowed.