0

I am currently trying to figure out why my program is not compiling. The assignment is for solving a maze, but I'm at the point where I need to display the maze to.

I am getting some very strange errors that my professor is not helping us with, they are as follows:

/tmp/cc0DImq3.o: In function `main':
main.cpp:(.text+0x57): undefined reference to `Maze::Maze(std::string)'
main.cpp:(.text+0x6f): undefined reference to `Maze::display()'
main.cpp:(.text+0x7b): undefined reference to `Maze::~Maze()'
main.cpp:(.text+0xae): undefined reference to `Maze::~Maze()'
main.cpp:(.text+0xbf): undefined reference to `Maze::~Maze()'
collect2: error: ld returned 1 exit status

main.cpp

    #include <iostream>
    #include <queue>
    #include "maze.h"

    using namespace std;

    struct Location{
        int row;
        int col;
    };

    // Starting point for the program, User types in a filename and then launches

    int main(){
        string fileName;
        cout << "Please Enter the name of the maze you would like to use:";
        cin >> fileName;

        Maze m(fileName);
        m.display();
    }

maze.h

#ifndef MAZE_H
#define MAZE_H

#include <string>
#include <iostream>

using namespace std;

class Maze{
    private: 
        char **mazeArray;   // the maze representation
        int sCol;           // The column for the starting point
        int sRow;           // The row for the starting point
        int gCol;           // The column for the goal point
        int gRow;           // The row for the goal point
        int width;          // The number of columns on the map 
        int height;         // The number of roww on the map
    public:
        Maze(string);
        ~Maze();
        void display();     //  Dsplay the maze, ? - is unexplored tile
        char explore( int, int ); // for exploring a particular position
        void setVisited( int, int ); // set position as visited

        int getWidth(){return width;}
        int getHeight(){return height;}
        int getStartRow(){return sRow;}
        int getStartCol(){return sCol;}
        int getGoalRow(){return gRow;}
        int getGoalCol(){return gCol;}
};

#endif // MAZE_H

maze.cpp

include <fstream>
#include "maze.h"

//Constructor
Maze::Maze(string filename){
    ifstream infile;
    infile.open(filename.c_str());

    if(!infile.fail()){
        // Obtain the width and Height
        infile >> width;
        infile >> height;

        mazeArray = new char*[height];
        for(int i = 0; i < height; i++){
            mazeArray[i] = new char[width];
        }
        infile.get(); // Make a newline

        // Loop for stepping through each position in array
        for(int i = 0; i < height; i++){
            for(int j = 0; j < width; j++){
                mazeArray[i][j] = (char)infile.get(); // obtains one character

                // check for the starting and goal points and same them
                if( mazeArray[i][j] == 'S'){
                    sCol = j;
                    sRow = i;
                } else if( mazeArray[i][j] == 'X'){
                    gCol = j;
                    gRow = i;
                }
            }
            // Makes a new line at the end of the array
            infile.get();
        }
    } else { // The file failed to open!
        cout << "Unable to open the maze file." << endl;
        //exit(1);
    }
    infile.close();
}

//Deconstructor 
Maze::~Maze(){
    for(int i = 0; i < height; i++){
        delete [] mazeArray[i];
     }
     delete [] mazeArray;
}

void Maze::display(){
    cout << endl; // Space before the array
    // Step through the 2d array and print each value
    int i, j;
    for(i = 0; i < height; i++){
        for(j = 0; j < width; j++){
            cout << mazeArray[i][j];
        }
        cout << endl;
    }
    cout << endl;
}

char Maze::explore( int row, int col){
    // Checks if the parametes are in the map
    if(row >= height || col >= width){
        cout << "Invalid: move out of bounds" << endl;
        return 'X'; // returns an error value.
    }
    return mazeArray[row][col]; // return what is in the location
}

void Maze::setVisited( int row, int col){
    if(mazeArray[row][col] == '.' || mazeArray[row][col] == 'S'){
        mazeArray[row][col] = '*' // set the space to visited
    }
}
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
FroyoJones
  • 41
  • 6
  • 4
    It sounds like you aren't linking or compiling all of your source files correctly. You probably aren't running the compiler with the right arguments. Can you show how you compile this? – François Andrieux Mar 04 '20 at 20:17
  • Do you mistakenly copy-paste `main.cpp` twice instead of `maze.h`? – Brian61354270 Mar 04 '20 at 20:19
  • I am just using our campus complier, just the "g++ main.cpp" after doing that its just giving me the errors. – FroyoJones Mar 04 '20 at 20:19
  • @FroyoJones You need to provide `g++` will **all** of the files needed to build your executable. Try `g++ main.cpp maze.cpp` – Brian61354270 Mar 04 '20 at 20:20
  • @Brian ... There we go, I dont know why I forgot that! Thank you so much! – FroyoJones Mar 04 '20 at 20:40
  • @Brian *Or* compile each file individually and subsequently link them all together. There's no rule saying you have to pass all source files to the compiler in one go. Building each compilation unit on its own and then linking them later is perfectly fine. – Jesper Juhl Mar 04 '20 at 20:43

0 Answers0