PLEASE HOW DO I CORRECT THE FOLLOWING ERRORS 1 USE OF UNDECL
PLEASE HOW DO I CORRECT THE FOLLOWING ERRORS:
1. (USE OF UNDECLARED IDENTIFIER ‘getWeight’)
2. (IMPLICIT CONVERSION LOSES INTEGER PRECISION ‘size_t’ (aka ‘unsingned long’) to int)
THESE ERRORS ARE SPECIFIED IN THE CODE WITH OPEN AND CLOSE BRACKETS.
NOTE: I\'m using Xcode (update 5) for this c++ program
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
// The following function is to get the precedence of the operators. High value means high priority. For non operators, //zero is returned.
int getPrecedence(char ch) {
switch (ch) {
case \'/\':
case \'*\': return 2;
case \'+\':
case \'-\': return 1;
default : return 0;
}
}
// Following function converts the infix expression to postfix expression
void infix_to_postfix(char infix[], char postfix[], int size) {
stack<char> s;
int weight;
int i = 0;
int k = 0;
char ch;
// iterate over the array containing the infix expression
while (i < size) {
ch = infix[i];
if (ch == \'(\') {
// If opening parenthesis is encountered, simply push
s.push(ch);
i++;
continue;
}
if (ch == \')\') {
// if a closing parenthesis is encountered, the pop all elements and append it to the postfix expression till we //encounter an opening parenthesis
while (!s.empty() && s.top() != \'(\') {
postfix[k++] = s.top();
s.pop();
}
// pop off the opening parenthesis also
if (!s.empty()) {
s.pop();
}
i++;
continue;
}
weight = getPrecedence(ch);
if (weight == 0) {
// zero precedence value means an operand. so, simply append it to postfix expression
postfix[k++] = ch;
}
else {
// If it is an operator
if (s.empty()) {
// If stack is empty, simply push the operator onto stack
s.push(ch);
}
else {
// pop of all the operators from the stack and
// append it to the postfix expression till we
// see an operator with a lower precedence that
// the current operator
while (!s.empty() && s.top() != \'(\' &&
weight <= getWeight(s.top())) { (USE OF UNDECLARED IDENTIFIER ‘getWeight’)
postfix[k++] = s.top();
s.pop();
}
// push the current operator onto stack
s.push(ch);
}
}
i++;
}
// pop of the remaining operators present in the stack
// and append it to postfix expression
while (!s.empty()) {
postfix[k++] = s.top();
s.pop();
}
postfix[k] = 0; // null terminate the postfix expression
}
// main
int main() {
char infix[] = \"A*(B+C)/D\";
int size = strlen(infix); (IMPLICIT CONVERSION LOSES INTEGER PRECISION ‘size_t’ (aka ‘unsingned long’) to int)
char postfix[size];
infix_to_postfix(infix,postfix,size);
cout<<\"\ Infix Expression :: \"<<infix;
cout<<\"\ Postfix Expression :: \"<<postfix;
cout<<endl;
return 0;
}
Solution
1) You need to use public int getweight()
{
return weight;
}
2)unsigned long is a 64-bit unsigned integer. and int is a 32-bit integer.
When building 32-bit applications, NSUInteger is a 32-bit unsigned integer. A 64-bit application treats NSUInteger as a 64-bit unsigned integer.
you can do :
or (if you are sure that your array will never contain more than 2^31-1 elements!), add an explicit cast:


