Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

c++ - How to solve 'No known conversion' in this code?

First of all I have to say that i have no experience with C My problem is I needed to write a make file to seperate 2 functions wrote like this before ;

#include <math.h>
#include <iostream>
using namespace std;

int main()
{
    float A,B,x,y;
    int   z;
    x=-0.62;
    y=0.82;
    z=25;
    A = pow(y, cbrt(double x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
    B = y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
    cout << "your answer for A = " << A << endl;
    cout << "your answer for B = " << B << endl;
}

In this point all is good and code is working , but when i try to seperate the functions like this its function A;

float A(float *x, float *y,int *z){
    A = pow(y, cbrt(double x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
    return A;
}

And its B;

#include <math.h>
#include <iostream>
using namespace std;

int B(float *x,*y,int *z){
    B = y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
    return B;
}

Here is my main function;

#include <math.h>
#include <iostream>
using namespace std;

int main()
{
    float A,B,x,y;
    int   z;
    x=-0.62;
    y=0.82;
    z=25;
    float A(x,y,z);
    float B(x,y,z); 
    cout << "your answer for A = " << A << endl;
    cout << "your answer for B = " << B << endl;

}

I get various amount of errors such as 'no known conversion' or 'pow not defined' how can i solve it ? I am pretty sure its about my variables and how i call them but.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this is what your looking for. There were multiple errors. Ill comment them out for you.

#include <math.h>
#include <iostream>
using namespace std;

// You dont need to pass variable pointers to a function like this.
float A(float x, float y, int z) {
    // When declaring a variable, make sure that you specify the return type.
    float A = pow(y, cbrt(x)) + sin(y - 3) * sin(y - 3) * sin(y - 3);
    return A;
}

// Plus, you dont need to include the header files (maths.h and iostream> before every function.
// Just include them at the beginning of the source file (or in the header file) and that's gonna be enough. 
// If your having the functions defined in different source files, you might need to include them once every file.

// You dont need to pass variable pointers to a function like this.
int B(float x, float y, int z) {
    // When declaring a variable, make sure that you specify the return type.
    float B =  y * (atan(z) - (atan(1) * 4 / 6)) / x + 1 / (pow(y, 2) + 1);
    return B;
}

int main()
{
    float x, y;
    int   z;
    x = -0.62;
    y = 0.82;
    z = 25;

    // Over here, dont name the variable with the same name as the function as the compiler will have a hard time 
    // undestanding whats going on.
    // Function calls are done like this: return_type variable = function_name(arguments);

    float a = A(x, y, z);
    float b = B(x, y, z);
    cout << "your answer for A = " << a << endl;
    cout << "your answer for B = " << b << endl;
}

Note: Pointers are basically memory addresses. You can pass pointers to a function where the value of the argument will be altered by the function. And you have to make sure you dereference it when you're working with the actual values (ie: *y) stored in the memory address. To get the address from a variable, you have to use the address-of (&) operator (ie: &y).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...