简体   繁体   English

C++ 错误 我需要帮助吗?

[英]C++ Error I need help please?

I have done a code which has to be written a mathemathical function and then give the images.我已经完成了一个必须编写一个数学函数然后给出图像的代码。 First, introduces the function as a string variable, but I don't know how to convert it in another type of variable to return the images of the function.首先,将函数作为字符串变量引入,但不知道如何将其转换为另一种类型的变量以返回函数的图像。

The compiler error I get is:我得到的编译器错误是:

cannot convert ' std::string {aka std::basic_string<char>} ' to ' const char* ' for argument ' 1 ' to ' int printf(const char*, ...) '不能将' std::string {aka std::basic_string<char>} ' 转换为 ' const char* ' 作为参数 ' 1 ' 到 ' int printf(const char*, ...) '

#include<iostream> 
#include<cmath> 
#include<math.h> 
#include<vector> 
#include<stdio.h> 
#include<iomanip> //setprecision// 
#include<sstream> 
#include<string> 
#define precisio 4 
#define K 100   
using namespace std;  
double valors(double a, double b);  
double g(double x);  
double x; 
double a = x; 
double y(x); 
int i;    
int main () {       
  cout << setprecision(precisio);   
  cout << "Escriu l'interval de la funció" << endl;         
  double a, b;      
  cout << "\n a =";     cin >> a;       
  cout << "\n b =";     cin >> b;       
  cout << "Escriu la funcio" << endl;   string s;   
  cin >> s;     cout << s;      
  double y = printf(s.c_str());         
  valors(a,b); 
}  

double valors(double a, double b){          
  int punts = K*(b-a) + 1;      
  double amplada = (b-a)/K;         
  cout << "\n\tx\tf(x)" << endl;        

  for (int i = 0; i < punts; i++) {                 
    cout << "\t" << a << "\t" << g(a) << endl;      
    a = a + amplada;    
  }     
}

double g(double x){
   return y; 
}
double valors(double a, double b)

您的函数不返回任何值,而应返回“double”。

No problem with this online compiler utilizing C++14: here这个使用 C++14 的在线编译器没问题:这里

#include<iostream>
#include<stdio.h>
#include<iomanip> //setprecision//
#include<sstream>
#include<string>
#define precisio 4
#define K 100
using namespace std;
double valors(double a, double b);
double g(double x);
double x;
double a = x;
double y(x);
int i;
int main()
{
    cout << setprecision(precisio);
    cout << "Escriu l'interval de la funció" << endl;
    double a, b;
    cout << "\n a =";
    cin >> a;
    cout << "\n b =";
    cin >> b;
    cout << "Escriu la funcio" << endl;
    string s;
    cin >> s;
    cout << s;
    double y = printf(s.c_str());
    valors(a, b);
}

double valors(double a, double b)
{
    int punts = K * (b - a) + 1;
    double amplada = (b - a) / K;
    cout << "\n\tx\tf(x)" << endl;

    for (int i = 0; i < punts; i++)
    {
        cout << "\t" << a << "\t" << g(a) << endl;
        a = a + amplada;
    }
}
double g(double x)
{
    return y;
}

I assume the purpose of :我假设的目的是:

cin >> s;   cout << s;
double y = printf(s.c_str());

was to put the value entered by the user on standard input into y.是将用户在标准输入上输入的值放入 y。 If so, you should just do:如果是这样,你应该这样做:

cin >> y;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM