Question

why do i get this syntax error? for example: i have these two classes: class Maze...

why do i get this syntax error?

for example: i have these two classes:

class Maze
{
private:
    int row, col;
    cell **arr;
public:
    Maze(int r = 0, int c = 0)
    {
        this->row = r; this->col = c;
        arr = new cell*[row];
        for (int i = 0; i < row; i++)
            arr[i] = new cell[col];
    }

};

class cell
{
private:
    bool visited;
    bool up, down, right, left;
    int x, y;
    int px, py;
    char status;
public:
    cell() {
        up = down = right = left = 0;
        x = y = 0;
        status = ' ';
    }
    bool getUp() { return up; }
    bool getDown() { return down; }
    bool getRight() { return right;  }
    bool getLeft() { return left; }
    bool getvisited() { return visited; }
    void setUp(bool b) { up = b; }
    void setDown(bool b) { down = b; }
    void setRight(bool b) { right = b; }
    void setLeft(bool b) { left = b; }

    void setvisited(bool b=0) { visited = b; }
    void setstatus(char s = ' ') { status = s; }
    char getStatus() { return status; }

};

and in main:

int main(){

Maze m (5,5);

m[1][1].setDown(false); //here error

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

setDown() method is a public function of class cell. But instance m is an object of class Maze. Hence object m can't call member function of another object. Using arr member field of Maze class, setDown method can be called. But it can't be called from main function, as arr member field is private. A proper member function inside Maze class should call the setDown() method using the arr field.

Thus the program should look like:

#include <iostream>

class cell
{
private:
bool visited;
bool up, down, right, left;
int x, y;
int px, py;
char status;
public:
cell() {
up = down = right = left = 0;
x = y = 0;
status = ' ';
}
bool getUp() { return up; }
bool getDown() { return down; }
bool getRight() { return right; }
bool getLeft() { return left; }
bool getvisited() { return visited; }
void setUp(bool b) { up = b; }
void setDown(bool b) { down = b; }
void setRight(bool b) { right = b; }
void setLeft(bool b) { left = b; }

void setvisited(bool b=0) { visited = b; }
void setstatus(char s = ' ') { status = s; }
char getStatus() { return status; }

};

class Maze
{
private:
int row, col;
cell ***arr;
public:
Maze(int r = 0, int c = 0)
{
this->row = r; this->col = c;
arr = new cell**[row];
for (int i = 0; i < row; i++)
arr[i] = new cell*[col];
for (int i = 0; i < row; i++)
for(int j=0; j< col; j++)
arr[i][j] = new cell();
}
void decreaseCell()
{
arr[1][1]->setDown(false);
}
};


int main()
{
Maze m(5,5);
m.decreaseCell();
}

Add a comment
Know the answer?
Add Answer to:
why do i get this syntax error? for example: i have these two classes: class Maze...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up...

    I NEED HELP IN MAZE PROBLEM. Re-write the following program using classes. The design is up to you, but at a minimum, you should have a Maze class with appropriate constructors and methods. You can add additional classes you may deem necessary. // This program fills in a maze with random positions and then runs the solver to solve it. // The moves are saved in two arrays, which store the X/Y coordinates we are moving to. // They are...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths...

    JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths possible in this maze. The program uses recursion. Can someone please explain to me why the program will pick one path if multiple traversable paths are available? Why does the program choose one path over a different path? (I believe it has something to do with the recursion terminating when a solution is found but I'm not sure.) Thank you!!!! The codes: public class...

  • This is java. Goal is to create a pacman type game. I have most of the...

    This is java. Goal is to create a pacman type game. I have most of the code done just need to add in ghosts score dots etc. Attached is the assignment, and after the assignment is my current code. import java.awt.Color; import java.awt.Dimension; import java.awt.Image; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class Maze extends JFrame implements KeyListener { private static final String[] FILE = {...

  • Create a communication information system for a company with using Red-Black Tree Method.

    PLEASE USE THE HEADER FILE THAT I ADDED TO THE END OF THE QUESTIONWrite this program with using C/C++, C#, Java or Python. Explain your code with comments. Create a communication information system for a company with using Red-Black Tree Method. Inthis system the users are allowed to search an employee from employee's registration number(id)and retreive the department number(id) and that employee's internal phone number. Also, if theemployee they are looking for is not in the tree, they can add it....

  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • In this question, you will test, using a backtracking algorithm, if a mouse can escape from...

    In this question, you will test, using a backtracking algorithm, if a mouse can escape from a rectangular maze. To ensure consistency of design, start your solution with maze_start.c. The backtracking algorithm helps the mouse by systematically trying all the routes through the maze until it either finds the escape hatch or exhausts all possible routes (and concludes that the mouse is trapped in the maze). If the backtracking algorithm finds a dead end, it retraces its path until it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT