-
April 23rd, 2012, 06:56 PM
#1
error: no match for ' ' problem
so there is something wrong with the compatibility of my expression variable with everything im trying to use it for. Do I need to create an alias for it to use it the way im am attempting to use it? or reference it as something else?
the build messages are saying "error: no match for operator!= in 'expression.std::vector<_TP, _Alloc>...ect
I am making a program to convert a give integer expression that is in regular notation and converting it to post fix
Code:
int arithmetic_expression::inputInfix(std::string inputString)
{
std::vector<std::string> expression = parse_string(inputString);
stack<int> intStack;
int output[100];
int n = 0;
int n1;
int *o;
o = &output[0];
while(expression[n] != 0){
if(isdigit(expression[n]) || isalpha(expression[n])){
*o = expression[n];
n++;
o++;
}
if(expression[n] == '('){
intStack.push(expression[n]);
n++
}
if(expression[n] == ')'){
n1 = intStack.top();
intStack.pop();
while(n1 != '('){
*o = n1;
o++;
n1 = intStack.top();
intStack.pop();
}
n++
}
if(expression[n] == '+' || expression[n] == '-' || expression[n] == '*' || expression[n] == '/' || expression[n] == '%'){
if(intStack.empty() == true){
intStack.push(expression[n]);
}else{
n1 = intStack.top();
intStack.pop();
while(priority(n1) >= priority(expression[n]){
*o = n1;
o++;
n1 = intStack.top();
intStack.pop();
}
intStack.push(n1);
intStack.push(expression[n]);
}
n++
}
while(!intStack.empty()){
*o = intStack.top();
o++
intStack.pop()
}
*o = '\0';
int valid = createExpressionTree(expression);
if (!valid) {
topPtr = NULL;
}
return(valid);
}
-
April 23rd, 2012, 07:15 PM
#2
Re: error: no match for ' ' problem
Code:
while(expression[n] != 0){
expression[n] is a string
use
Code:
while(!expression[n].empty()){
kurt
EDIT: i see you seem to think it's a char. There are more errors like this like
Code:
if(isdigit(expression[n]) || isalpha(expression[n])){
Last edited by ZuK; April 23rd, 2012 at 07:18 PM.
-
April 23rd, 2012, 07:30 PM
#3
Re: error: no match for ' ' problem
how would I go about formatting the isdigit and isalpha line to make it a string instead?
-
April 23rd, 2012, 09:13 PM
#4
Re: error: no match for ' ' problem
Originally Posted by chucker
how would I go about formatting the isdigit and isalpha line to make it a string instead?
isdigit is a function that tests a single character to see if it is a digit. Similarly, the isalpha tests a single character to see if it is an alphabetic character.
So there is no way to "format" these functions to do something they are not written to do. You have to write your own routine to test if an entire string is whatever you feel it should be.
Regards,
Paul McKenzie
-
April 23rd, 2012, 09:22 PM
#5
Re: error: no match for ' ' problem
that sounds reasonable. But generally how do I go about formatting my expression[n] variable to be recognized as a string? Like when it is not empty
-
April 23rd, 2012, 10:16 PM
#6
Re: error: no match for ' ' problem
like at the line where it says:
Code:
if(expression[n] == '('){
should it be expression[n].parse_string()?
-
April 23rd, 2012, 11:13 PM
#7
Re: error: no match for ' ' problem
Originally Posted by chucker
that sounds reasonable. But generally how do I go about formatting my expression[n] variable to be recognized as a string? Like when it is not empty
You have a vector of string. The name of the vector is expression. Each element in that vector is an entire string. So expression[0] is a string, expression[1] is a string, expression[2] is a string.
Well, given a string how do you access a character? You use operator [] on that string. I believe you erroneously used operator [] to access each string in the vector. You need to go one more level to access the actual characters of the string:
Code:
(expression[0]) [0] --> the first character in string 0
(expression[0]) [1] --> the second character in string 0
(expression[1]) [0] --> the first character in string 1
(expression[1]) [1] --> the second character in string 1
etc...
You can remove the parentheses -- I added them so that you can understand what is being done to access each character.
Regards,
Paul McKenzie
-
April 24th, 2012, 12:03 AM
#8
Re: error: no match for ' ' problem
I didnt get any compile errors but Im not sure how to test it without putting it into a tree expression tree yet. Did I implement it the way you meant?
Code:
int arithmetic_expression::inputInfix(std::string inputString)
{
std::vector<std::string> expression = parse_string(inputString);
stack<string> charStack;
int n = 0;
while(!expression[n].empty()){
if(expression[n][0] == '('){
charStack.push(expression[n]);
n++;
}
if(expression[n][0] == ')'){
expression[n] = charStack.top();
charStack.pop();
while(expression[n][0] != '('){
expression[n] = charStack.top();
charStack.pop();
}
n++;
}
if(expression[n][0] == '+' || expression[n][0] == '-' || expression[n][0] == '*' || expression[n][0] == '/'){
if(charStack.empty() == true){
charStack.push(expression[n]);
}else{
expression[n] = charStack.top();
charStack.pop();
}
n++;
}
-
April 24th, 2012, 12:39 AM
#9
Re: error: no match for ' ' problem
okay here is the real code actually, and i did try and test if the conversion to RPN would print out okay. but for example one of the inputs is "31+25*2"; and all that my code could spit out is "31"
Code:
int arithmetic_expression::inputInfix(std::string inputString)
{
std::vector<std::string> expression = parse_string(inputString);
stack<string> charStack;
int n = 0;
while(!expression[n].empty()){
if(expression[n][0] == '('){
charStack.push(expression[n]);
n++;
}
if(expression[n][0] == ')'){
expression[n] = charStack.top();
charStack.pop();
while(expression[n][0] != '('){
expression[n] = charStack.top();
charStack.pop();
}
n++;
}
if(expression[n][0] == '+' || expression[n][0] == '-' || expression[n][0] == '*' || expression[n][0] == '/'){
if(charStack.empty() == true){
charStack.push(expression[n]);
}else{
expression[n] = charStack.top();
charStack.pop();
}
n++;
}
cout << "after" << endl;
cout << expression[n];
int valid = createExpressionTree(expression);
if (!valid) {
topPtr = NULL;
return(valid);
}}}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|