Expected Expression Error in Xcode - c++

I am making a move function for a game and I get a expected expression error I can't figure out why, it seems legal what i did.
void Ant::move()
{
int dir=rand()%4;
if (dir==0)
{
if ((y>0) && (world->getAt(x,y-1)==NULL))
{
world->setAt(x,y-1,world->getAt(x,y));
world->setAt(x,y,NULL);
y++;
}
}
else
{
if ((x<WORLDSIZE-1) && (world->getAt(x+1,y)==NULL))
{
world->setAt(x-1,y,world->getAt(x,y));
world->setAt(x,y,NULL);
x--;
}
}
else
{
if ((x<WORLDSIZE-1) && (world-getAt(x+1,y)==NULL))
{
world->setAt(x+1,y,world->getAt(x,y));
world->setAt(x,y,NULL);
x++;
}
}
}
The problem is the second else call.

I think the problem is:
world-getAt(x+1,y)==NULL
You forgot the >
world->getAt(x+1,y)==NULL
In the second if statement.

There is an if missing after the first else. You have now
if {
...
} else { // here you need an if - or revise the structure
} else {
}
For instance try ...
void Ant::move()
{
int dir=rand()%4;
if (dir==0)
{
if ((y>0) && (world->getAt(x,y-1)==NULL))
{
world->setAt(x,y-1,world->getAt(x,y));
world->setAt(x,y,NULL);
y++;
} else
if ((x<WORLDSIZE-1) && (world->getAt(x+1,y)==NULL))
{
world->setAt(x-1,y,world->getAt(x,y));
world->setAt(x,y,NULL);
x--;
} else
if ((x<WORLDSIZE-1) && (world-getAt(x+1,y)==NULL))
{
world->setAt(x+1,y,world->getAt(x,y));
world->setAt(x,y,NULL);
x++;
}
}
}

Related

Error C4716: recordDownLog: must return a value

void *recordDownLog(void *args) {
while (!isFinished && !isWaitFinished) {
isFinished = true;
int i = 0;
Sleep(sleepTime);
while (i <= downNum) {
if (downEndSize[i]<maxSize[i]) {
isFinished = false;
}
i++;
}
if (isFinished || isEnded) {
if (isFinished) {
writeDownFile(1);
}
else {
writeDownFile(2);
}
}
else {
writeDownFile(0);
}
}
cout << "end" << endl;
isWaitFinished = true;
}
what should i return the value.
If you don't need to return any value, then change the return type to void. But if there is a missing return statement, then you'd know better than me where that should go. Maybe you want to return args, because that isn't used anywhere else and is the same type as the current return type.

error: no matching function for call to c++ enumeration

I have the following problem already longer time. The point ist that I have read a bit on stackoverflow and I have used a typedef, but I doesnt help me.
Looking forward to get some help :)
Align_vector.h :
#include <utils/vector_2d.h>
class Vector_2d;
namespace Utils {
class Align_vector : public Vector_2d {
protected:
bool check_range(int x, int y);
public:
typedef enum {left, right, up, down} Alignment;
Align_vector(Alignment alignment);
void set_alignment(Alignment alignment);
Alignment get_alignment();
};
} /* namespace Utils */
Align_vector.cc :
#include <utils/align_vector.h>
namespace Utils {
Align_vector::Align_vector(Alignment alignment) {
this->alignment = alignment;
}
void set_alignment(Alignment alignment) {
Align_vector::alignment = alignment;
}
Alignment get_alignment() {
return Align_vector::alignment ;
}
bool check_range(int x, int y) {
switch ( Align_vector::alignment ) {
case Align_vector::left:
if (x == -1 && y == 0) {
return true;
} else {
return false;
}
break;
case Align_vector::right:
if (x == 1 && y == 0) {
return true;
} else {
return false;
}
break;
case Align_vector::down:
if (x == 0 && y == -1) {
return true;
} else {
return false;
}
break;
case Align_vector::up:
if (x == 0 && y == 1) {
return true;
} else {
return false;
}
break;
default:
return false;
break;
}
}
} /* namespace Utils */
Here is the error:
/utils/align_vector.cc:14:47: error: no matching function for call to ‘Utils::Vector_2d::Vector_2d()’
Align_vector::Align_vector(Alignment alignment) {
This code has numerous issues. First of all, you have defined an
enum called Alignment, but you did not declare a member
of that type. To do so, add this line after the definition of the enum:
Alignment alignment;
Your definitions of methods are also incorrect, the alignment is
supposed to belong to a specific object, while you are using it in
several functions as if it were a static member of the class. Here
are the fixed versions of method definitions:
namespace Utils {
Align_vector::Align_vector(Alignment alignment) {
this->alignment = alignment;
}
void Align_vector::set_alignment(Alignment alignment) {
this->alignment = alignment;
}
Align_vector::Alignment Align_vector::get_alignment() {
return this->alignment;
}
bool Align_vector::check_range(int x, int y) {
switch(this->alignment) {
case Align_vector::left:
if(x == -1 && y == 0) {
return true;
}
else {
return false;
}
break;
case Align_vector::right:
if(x == 1 && y == 0) {
return true;
}
else {
return false;
}
break;
case Align_vector::down:
if(x == 0 && y == -1) {
return true;
}
else {
return false;
}
break;
case Align_vector::up:
if(x == 0 && y == 1) {
return true;
}
else {
return false;
}
break;
default:
return false;
break;
}
}
} /* namespace Utils */
Finally, you are missing a definition of a default constructor for base
class Vector_2d (your comments suggest that you did not define that class
at all, you just declared its existance using statement class Vector_2d;).
Judging by your overall implementation, I think user #molbdnilo was correct
when suggesting you should learn more about C++ programming in general.
Hope this helps!

Dijkstra' s Shunting Yard algorithm in C++

As my homework I have to write a program that calculates an equation given as string. A part of the program is a function parsing an infix expression into postfix. Here is my code:
void dijkstra(string& s)
{
int i,j=s.size();
stack < char > znaki;
string output=" ";
string znak;
for(i=0; i<j;++i)
{
if((int(s[i])>47&&int(s[i])<58)||s[i]=='.')
{
output+=s[i];
}
else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
{
output+=" ";
if(znaki.empty())
{
}
else
{
if((s[i]=='+'||s[i]=='-'))
{
znak=znaki.top();
znaki.pop();
output+=znak;
}
else if(s[i]=='*')
{
if(znaki.top()=='*'||znaki.top()=='/')
{
znak=znaki.top();
znaki.pop();
output+=znak;
}
}
else if(s[i]=='/')
{
if(znaki.top()=='*'||znaki.top()=='/')
{
znak=znaki.top();
znaki.pop();
output+=znak;
}
}
}
znaki.push(s[i]);
}
else if(int(s[i])=='(')
{
znaki.push(s[i]);
}
else if(int(s[i])==')')
{
while(znaki.top()!='(')
{
output+=znaki.top();
znaki.pop();
}
znaki.pop();
}
}
while(znaki.empty()!=true)
{
output+=znaki.top();
znaki.pop();
}
s=output;
}
The problem is that it works in all conditions except equations like 4/6. Any ideas? I've just made requested updates

Parsing Fully Parenthesized expression

I'm trying to Parse a Fully Parenthesized Exp for this grammar:
exp->(exp + exp)
|(exp - exp)
| num
num->[0-9]
...but I have a problem: when I enter "1+4" no error appears. How can I solve it ??
This is a classic problem of recursive descent parsers: your grammar does not require that the entire input is consumed. Once it finishes with the "while isdigit" loop, it considers its job done, ignoring the +4 portion of the input.
Add a check that the end of line is reached after the call of the top-level expression to address this problem:
void TopExp() {
Expr();
Match('\n');
}
You need to modify Match to allow matching \n when no additional input is available:
void Match(char c) {
int p = cin.peek();
if(p==c) {
cin>>c;
} else if (p == EOF && c == '\n') {
return
} else {
cout<<"Syntax Error! "<<(char)cin.peek()<<endl;
cin.ignore();
}
}
Try with this changed instruction: if (openParenthesis-closeParenthesis>0)
int Match(char c)
{
if(cin.peek()==c) {
cin>>c;
return 1;
} else
{
cout<<"Syntax Error! "<<(char)cin.peek()<<endl;
cin.ignore();
return 0;
}
}
void MatchOp()
{
if(cin.peek()=='+')
Match('+');
else if(cin.peek()=='-')
Match('-');
else
{
cout<<"invalid Operation: "<<(char)cin.peek()<<endl;
cin.ignore();
}
}
void Exp()
{ static int openParenthesis = 0;
static int closeParenthesis = 0;
if(cin.peek()!='\n')
if(cin.peek()=='(')
{
if (Match('(') == 1) {
openParenthesis += 1;
}
Exp();MatchOp();Exp();
if (Match(')') == 1) {
closeParenthesis -= 1;
}
}
else if(isdigit(cin.peek()))
{
if (openParenthesis-closeParenthesis>0) {
cout<<"Syntax Error! "<<(char)cin.peek()<<endl;
cin.ignore();
} else {
while(isdigit(cin.peek()))
{ cout<<(char)cin.peek();
Match(cin.peek());
}
}
}
else
{
cout<<"Syntax Error!"<<(char)cin.peek()<<endl;
cin.ignore();
}
}

Optimizing nested if/ then, java

This code's only redeeming quality is that it works. Can you please help me structure it better?
if (profile.isIgnoreCase()) {
// ignore case
if (masterKey.equalsIgnoreCase((targetKey))) {
if (masterValue.equalsIgnoreCase(targetValue)) {
doOK(masterKey, masterValue);
break;
} else {
// Key is either Missing or is an Error
if (checkErrors) {
doError(masterKey, masterValue, targetValue);
break;
}
}
}
} else {
if (masterKey.equals(targetKey)) {
if (masterValue.equals(targetValue)) {
doOK(masterKey, masterValue);
break;
} else {
if (checkErrors) {
doError(masterKey, masterValue, targetValue);
break;
}
}
}
}
You can remove some of the repetition by using:
if (profile.isIgnoreCase()) {
masterKey = masterKey.toLowerCase();
masterValue = masterValue.toLowerCase();
}
if (masterKey.equals(targetKey)) {
if (masterValue.equals(targetValue)) {
doOK(masterKey, masterValue);
} else {
if (checkErrors) {
doError(masterKey, masterValue, targetValue);
}
}
}
I have also removed the breaks as I doesn't look like you need them to me
[UPDATE] Alternatively, how about writing a new method to handle the comparison
public boolean isEqual(String a, String b, boolean ignoreCase) {
if (ignoreCase) {
return a.equalsIgnoreCase(b);
} else {
return a.equals(b);
}
}
you would then update your code like so:
if (isEqual(masterKey,targetKey,profile.isIgnoreCase())) {
if (isEqual(masterValue,targetValue,profile.isIgnoreCase())) {
doOK(masterKey, masterValue);
} else {
if (checkErrors) {
doError(masterKey, masterValue, targetValue);
}
}
}
Pull out the key and value comparisons into local variables and you can eliminate the duplicated logic. This avoids modifying the strings, and as a bonus makes the if statements a bit easier on the eyes.
boolean keysMatch, valuesMatch;
if (profile.isIgnoreCase()) {
keysMatch = masterKey .equalsIgnoreCase(targetKey);
valuesMatch = masterValue.equalsIgnoreCase(targetValue);
} else {
keysMatch = masterKey .equals(targetKey);
valuesMatch = masterValue.equals(targetValue);
}
if (keysMatch) {
if (valuesMatch) {
doOK(masterKey, masterValue);
break;
} else {
// Key is either Missing or is an Error
if (checkErrors) {
doError(masterKey, masterValue, targetValue);
break;
}
}
}
You could make it shorter by replacing them with the booleans that they represent, ie: masterKey.equalsIgnoreCase((targetKey)) with true or false.Which could help make it shorter, because you would need one less if and else clause.
bool ignoreCase = profile.isIgnoreCase();
if ((ignoreCase && masterKey.equalsIgnoreCase(targetKey)) ||
(!ignoreCase && masterKey.equals(targetKey))){
if ((ignoreCase && masterValue.equalsIgnoreCase(targetValue)) ||
(!ignoreCase && masterValue.equals(targetValue))) {
doOK(masterKey, masterValue);
break;
} else
if (checkErrors) {
doError(masterKey, masterValue, targetValue);
break;
}
}
Should be quicker this way to read boolean value, if this evaluates to false the program can skip to the next evaluation sooner than running the string compare first.
don't know if you need the break there. But I believe this is completely equivalent to your code.
bool ignoreCase = profile.isIgnoreCase();
if(ignoreCase and masterKey.equalsIgnoreCase(targetKey) or !ignoreCase and masterKey.equals(targetKey)) {
if(ignoreCase and masterValue.equalsIgnoreCase(targetValue) or !ignoreCase and masterValue.equals(targetValue)) {
doOK(masterKey, masterValue);
break;
} else if(checkErrors) {
doError(masterKey, masterValue, targetValue);
break;
}
}
Or I would write the following function in masterKey and masterValue class
public bool equalsCheckCase(targetKey, ignoreCase) {
if(ignoreCase) {
return this.equalsIgnoreCase(targetKey)
} else {
return this.equals(targetKey);
}
}
So the code sample becomes more readable.
bool ignoreCase = profile.isIgnoreCase();
if(masterKey.equalsCheckCase(targetKey, ignoreCase)) {
if(masterValue.equalsCheckCase(targetValue, ignoreCase)) {
doOK(masterKey, masterValue);
break;
} else if(checkErrors) {
doError(masterKey, masterValue, targetValue);
break;
}
}