1

This is a sample of code that I've written for a school science fair.

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

struct FUNC{
    char token;
    FUNC *left;
    FUNC *right;
};

double eval (FUNC *head){
    if (head->left==NULL){ 
        return atof(head->token); //this is where the error occurs
    }
}

void main(){
    FUNC node1= {'1',NULL,NULL};

    cout << eval(&node1)<< endl;

    system("pause");
}

When I run this code I receive this error.

error C2664: 'atof' : cannot convert parameter 1 from 'char' to 'const char *'

Can anyone explain this error and give me an example on how to remedy it?

Mysticial
  • 464,885
  • 45
  • 335
  • 332
Stem
  • 11
  • 1

3 Answers3

2

It's simple. You are passing a char to a function that expects a char*.

struct FUNC{
    char token;
    FUNC *left;
    FUNC *right;
};

should be

struct FUNC{
    char* token;
    FUNC *left;
    FUNC *right;
};

and while you are at it you also would have to initialize the char* so you would have to make a function like

   FUNC* initFunc(const char* str,FUNC* left,FUNC* right)
   {
       // (FUNC*) is a cast to a type of pointer to FUNC. It is not needed if you write in C but 
       //since I saw cout in your code then if it's C++ you need to cast the results of malloc
       FUNC* ret = (FUNC*) malloc(sizeof(FUNC);
       int len = strlen(str);
       ret->str = malloc(len+1);
       strcpy(ret->str,str);
       ret->left = left;
       ret->right = right;
       return ret;
   }

So finally in your main you would have something like this:

//please note the existence of " " since this is not a char but a string literal
FUNC* node1 = initFunc("1",NULL,NULL);

cout << eval(node1)<< endl;
Lefteris
  • 3,196
  • 5
  • 31
  • 52
1

A bit of advise, you should include the header cmath in place of math.h. Quoting GOTW

""Namespace Rule #3: Use C headers with the new style "#include <cheader>" instead of the old style "#include <header.h>".

For backward compatibility with C, C++ still supports all of the standard C header names (e.g., stdio.h), and when you #include those original versions the associated C library functions are visible in the global namespace as before--but in the same breath C++ also says that the old header names are deprecated, which puts the world on notice that they may be removed in a future version of the C++ standard. Thus Standard C++ strongly encourages programmers to prefer using the new versions of the C headers that start with "c" and drop the ".h" extension (e.g., cstdio); when you #include the C headers using the new names, you get the same C library functions, but now they live in namespace std." "

bisarch
  • 1,388
  • 15
  • 31
0

To get the numeric value of a single character, just use:

c - '0'

Not sure why you were going to use atof anyway, for a number that isn't fractional. The function of choice for parsing strings into whole numbers should usually be strtod, but as mentioned, with one character you don't need even that.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • The eval function isn't complete because I was stuck. But I need the value as a double because you can't divide an int value. The FUNC structure is an operator tree so I need the char type there because the tree needs to contain chars like '/' for division – Stem Feb 12 '12 at 06:42
  • @WilliamInasuitBolduc: You can `return head->token - '0';` and the compiler will automatically convert to the return type, double. – Ben Voigt Feb 12 '12 at 06:43