No matching function in Calculator Program

I am trying to make an infix calculator but I keep hitting a problem saying:

error: no matching function for call to ‘val_in(token&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int&)’

in this line :
double b= strtod(a.c_str() + i, 0);
from the code below.

Help is greatly appreciated. Thanks in advance


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



template <typename T>

	token val_in(token &a, double b)
	{
		a.val=b;
		a.isOp=false;
	}
	token op_in(token &a, char b)
	{
		a.op=b;
		a.isOp=true; 
	}

vector <token> tokenize(string &a)
{
	vector <token> ret;

	for (int i=0; i<a.size(); i++)
	{

		 if (is_operand(a[i]))
		{
			token temp;
			ret.push_back(op_in(temp,a[i]));
		}

		else if (is_num(a,i))
		{
			token temp;
			double b= strtod(a.c_str() + i, 0);
			ret.push_back (val_in(temp,b));

			while (is_num(a,i) && is_num(a,i+1))
			{
				i++;
			}

			cout << endl;
		}

		else 
		{
			cout << "Skipping Unknown Character" << a[i] << endl; 
		}
	}
	return ret; 
}
	

Last edited on
how could you get the error for that link mentioned? They are two different functions. The error is somewhere when you call the val_in function.

Somewhere you are trying to pass : (token&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int& or: token& , std::string& , int& instead of token &, double


Thinking about it now..If you are saying the error is on that line.. Then the error is probably inside of your strtod function. Can we see the strtod function because you are probably calling val_in wrong in there. didn't realize strtod was older version of atof
Last edited on
Here the whole program. Still trying to get the tokenization done before advancing. Thanks for the help again.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cctype>
#include <cmath>


using namespace std;

struct token
{
	double val;
	char op;
	bool isOp; 
};

bool is_operand(char c)
{
	return (c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/' || c == '<' || c== '>' || c == '{'
		|| c== '}' || c== '[' || c == ']' || c == '^');
}

bool is_num(string &a, int i)
{
	if (isdigit(a[i]) || a[i]=='.' || (a[i]=='-' && isdigit(a[i+1]) && (!isdigit(a[i-1]) || i==0)  ) )
	{
		return true;
	}
	return false;
	
}

double solve (char op, double lhs, double rhs)
{
	switch (op)
	{
		case '^':
			return pow (lhs,rhs);
		case'*':
			return lhs*rhs;
		case '/':
			return lhs/rhs;
		case '+':
			return lhs+rhs;
		case '-':
			return lhs-rhs;
		default:
			cout << "invalid operator" << endl;
			break;
	}
}


template <typename T>

	token val_in(token &a, double b)
	{
		a.val=b;
		a.isOp=false;
	}
	token op_in(token &a, char b)
	{
		a.op=b;
		a.isOp=true; 
	}

vector <token> tokenize(string &a)
{
	vector <token> ret;

	for (int i=0; i<a.size(); i++)
	{

		 if (is_operand(a[i]))
		{
			token temp;
			ret.push_back(op_in(temp,a[i]));
		}

		else if (is_num(a,i))
		{
			token temp;
			double b= strtod(a.c_str() + i, 0);
			ret.push_back (val_in(temp,b));

			while (is_num(a,i) && is_num(a,i+1))
			{
				i++;
			}

			cout << endl;
		}

		else 
		{
			cout << "Skipping Unknown Character" << a[i] << endl; 
		}
	}
	return ret; 
}
	

int main ()
{

	string input;
	while (getline(cin,input))
	{
		vector <token> tokens =tokenize (input);
		cout << endl; 
		cout << tokens.size() << endl;
		for (int i=0; i<tokens.size(); i++)
		{
			if(tokens[i].isOp)
			{
				cout << tokens[i].op << endl;
			}
			else 
			{
				cout << tokens[i].val << endl; 
			}
		}
	}




}
Not sure what you were trying to do with the template <typename T> on line 56, but remove that and your code will compile.
However, it probably won't run correctly, since neither val_in nor op_in return any value when they're supposed to return a token.
Apparently forgot to put the return in the function. Now it works. Thanks for the help
Topic archived. No new replies allowed.