• 0

C++ linking functions in class


Question

grid.h

#include <vector>
#include <string>

using namespace std;

class grid
{
 ?// A class which manipulates a 2-dimensional grid of bool
 ?
 ? public :
 ? ? ? grid(); ?
 ? ? ? // default constructor - initialises variables 
 ? ? ? void readGrid(string filename);
 ? ? ? // precondition : none
 ? ? ? // postcondition :
 ? ? ? // if a file called filename exists then the file of grid
 ? ? ? // data has been read in 
 ? ? ? // otherwise the message ?"File does not exist" is
 ? ? ? // displayed 
 ? ? ? void printGrid();
 ? ? ? // displays the grid data with each row on a new line
 ? ? ? int getMaxBlob();
 ? ? ? // precondition : isEmpty is false and findAllBlobs has been invoked
 ? ? ? // postcondition : returns the size of the maximum blob
 ? ? ? bool isEmpty();
 ? ? ? // returns true if grid data is empty (not successfully read in)
 ? ? ? // and otherwise false
 ? ? ? void findAllBlobs();
 ? ? ? // precondition : isEmpty is false 
 ? ? ? // postcondition : the size of each blob has been determined
 ? ? ? // and displayed (integers separated by tabs)
 ? ? ? // the maximum blob size has been determined.
 ? ? ? 
 ? protected :
 ? ? ? int getBlobSize(int curRow, int curCol);
 ? ? ? // recursive function called by findAllBlobs()
 ? ? ? // returns the size of the current blob 
 ? ? ? // (a blob starting at row CurRow and column curCol).

 ? private :
 ? ? ? vector < vector <bool> > ?v; ? // the 2-dimensional grid of data
 ? ? ? int rows; ? ? ? ? ? ? ? ? ? ? ?// the number of rows in the grid data
 ? ? ? int cols; ? ? ? ? ? ? ? ? ? ? ?// the number of columns in the grid data
 ? ? ? int maxBlob; ? ? ? ? ? ? ? ? ? // the size of the maximum blob found
 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// (set by findAllBlobs()
}; ? ?

grid.cpp

// File: grid.cpp
// Description: implementation file for the class 'grid.h'
 ?
#include "grid.h" ? ? 
#include <fstream>
#include <iostream>
 ?
grid::grid()
{
 ?rows = 0; ? ? // initalises the row to be 0
 ?cols = 0; ? ? // initialises the column to be 0
 ?maxBlob = 0; ?// initialises the maximum blob to be 0
} // end grid() (default constructor)
 ?
void grid::readGrid(string filename)
{
 ?// stores the state of a cell as '.' (empty) or '*' (filled) 
 ?char cell; ?
 ?// declares inputGrid as an input file stream
 ?ifstream inputGrid;
 ? ?
 ?inputGrid.open(filename.c_str());
 ?// if file exists
 ?if (inputGrid.is_open())
 ?{
 ? ?// gets the dimensions of the grid
 ? ?inputGrid >> rows >> cols;
 ? ?// resizes the two dimensional vector to the dimensions
 ? ?// specified by the input file
 ? ?v.resize(rows, vector<bool> (cols, false));
 ? ?
 ? ?// row counter of the two dimensional vector (v)
 ? ?for (int row = 0; row < rows; row++)
 ? ?{
 ? ? ?// column counter of the two dimensional vector (v)
 ? ? ?for (int col = 0; col < cols; col++)
 ? ? ?{
 ? ? ? ?inputGrid >> cell;
 ? ? ? ?// if cell is not filled (.)
 ? ? ? ?if (cell == '.')
 ? ? ? ?{
 ? ? ? ? ?v[row][col] = false;
 ? ? ? ?} ?
 ? ? ? ?// if cell is filled (*)
 ? ? ? ?else if (cell == '*')
 ? ? ? ?{
 ? ? ? ? ?v[row][col] = true;
 ? ? ? ?} // end else ?
 ? ? ?} // end currenct column
 ? ?} // end current row
 ? ?inputGrid.close();
 ?} // end check if file is opened ?
 ?else
 ?{
 ? ?cout << "File does not exist" << endl;
 ?} ? ?
} // end readGrid

void grid::printGrid()
{
 ?// row counter of the two dimensional vector (v)
 ?for (int row = 0; row < rows; row++)
 ?{
 ? ?// column counter of the two dimensional vector (v)
 ? ?for (int col = 0; col < cols; col++)
 ? ?{
 ? ? ?// if the cell is empty
 ? ? ?if (v[row][col] == false)
 ? ? ?{
 ? ? ? ?cout << '.';
 ? ? ?} ?
 ? ? ?else
 ? ? ?{
 ? ? ? ?cout << '*';
 ? ? ?} ?
 ? ?} // end printing one row of the grid
 ? ?cout << endl;
 ?} // end printing the whole grid 
} // end printGrid()

bool grid::isEmpty()
{
 ?// uses the empty() function to determine if the ?vector is empty
 ?return v.empty();
} // end isEmpty()

int grid::getMaxBlob()
{
 ?// returns the size of the largest blob
 ?return maxBlob;
} // end getMaxBlob()

int grid::getBlobSize(int curRow, int curCol)
{
 ?// if position of the cell at (curRow, curCol) is not on the grid
 ?// OR if the cell is not filled 
 ?if (curRow < 0 || curRow > rows - 1 ||
 ? ? ?curCol < 0 || curCol > cols - 1 ||
 ? ? ?v[curRow][curCol] == false)
 ?{
 ? ?return 0;
 ?} // end if
 ?else
 ?{
 ? ?// make the current cell unfilled
 ? ?v[curRow][curCol] = false;
 ? ?return 1 + getBlobSize(curRow - 1, curCol - 1) // gets blob size of top left neighbour
 ? ? ? ? ? ? + getBlobSize(curRow - 1, curCol) ? ? // gets blob size of top neighbour 
 ? ? ? ? ? ? + getBlobSize(curRow - 1, curCol + 1) // gets blob size of top right neighbour 
 ? ? ? ? ? ? + getBlobSize(curRow, curCol - 1) ? ? // gets blob size of left neighbour
 ? ? ? ? ? ? + getBlobSize(curRow, curCol + 1) ? ? // gets blob size of right neighbour
 ? ? ? ? ? ? + getBlobSize(curRow + 1, curCol - 1) // gets blob size of bottom left neighbour
 ? ? ? ? ? ? + getBlobSize(curRow + 1, curCol) ? ? // gets blob size of bottom neighbour ? ? 
 ? ? ? ? ? ? + getBlobSize(curRow + 1, curCol + 1);// gets blob size of bottom right neighbour
 ?} // end else ?
} // end getBlobSize() ?

void grid::findAllBlobs()
{ ?
 ?// blobSize stores the size of a blob
 ?int blobSize = 0;
 ?
 ?// if the grid is NOT empty
 ?if (!isEmpty())
 ?{
 ? ?// row counter of the two dimensional vector (v)
 ? ?for (int row = 0; row < rows; row++)
 ? ?{
 ? ? ?// column counter of the two dimensional vector (v)
 ? ? ?for (int col = 0; col < cols; col++)
 ? ? ?{
 ? ? ? ?// if a filled cell is found
 ? ? ? ?if (v[row][col] == true)
 ? ? ? ?{
 ? ? ? ? ?blobSize = getBlobSize(row, col);
 ? ? ? ? ?// a blob is only considered a blob if there are
 ? ? ? ? ?// at least 2 cells connected to each other
 ? ? ? ? ?if (blobSize > 1)
 ? ? ? ? ?{
 ? ? ? ? ? ?cout << blobSize << '\t';
 ? ? ? ? ?} ?
 ? ? ? ? ?// if the current blob is larger than the blob 
 ? ? ? ? ?// stored in maxBlob
 ? ? ? ? ?if (blobSize > maxBlob)
 ? ? ? ? ?{
 ? ? ? ? ? ?maxBlob = blobSize;
 ? ? ? ? ?} ? 
 ? ? ? ?} // end if 
 ? ? ?} // end for
 ? ?} // end for
 ? ?cout << endl;
 ?} // end isEmpty() check ? ?
} // end findAllBlobs() ? ?

blob.cpp

#include <iostream>
#include <cstdlib>
#include <fstream>
#include "grid.h"
using namespace std;

int main()
{
  string filename;
  // stores the name of the file used for blob
  grid data;
  // declares data for class 'grid'                 

  cout << "Enter filename: " << endl;
  cin >> filename;
  //user enters in the filename
  data.readGrid(filename);
  // reads in the grid, specified by the file name (if grid is not empty)

  data.printGrid();
  // prints out the data and size of each blob and determines largest blob size
  data.findAllBlobs();
  
  if (!data.isEmpty())
  {
    cout << "The maximum blob size is " 
         << data.getMaxBlob() << endl;
    // displays the max blob size of the grid
  }     
  
  system("PAUSE");	
  return 0;
}

now, my problem is, i have to compile from blob.cpp

and this is the error msg

C:\DOCUME~1\JIMMYL~1\LOCALS~1\Temp\ccW0baaa.o(.text+0x8b)://c/docume~1/jimmyl~1/desktop/bigfol~1/testin~1/blobs.cpp: undefined reference to `grid::grid(void)'
C:\DOCUME~1\JIMMYL~1\LOCALS~1\Temp\ccW0baaa.o(.text+0x10e)://c/docume~1/jimmyl~1/desktop/bigfol~1/testin~1/blobs.cpp: undefined reference to `grid::readGrid(basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0> >)'
C:\DOCUME~1\JIMMYL~1\LOCALS~1\Temp\ccW0baaa.o(.text+0x11d)://c/docume~1/jimmyl~1/desktop/bigfol~1/testin~1/blobs.cpp: undefined reference to `grid::printGrid(void)'
C:\DOCUME~1\JIMMYL~1\LOCALS~1\Temp\ccW0baaa.o(.text+0x12c)://c/docume~1/jimmyl~1/desktop/bigfol~1/testin~1/blobs.cpp: undefined reference to `grid::findAllBlobs(void)'
C:\DOCUME~1\JIMMYL~1\LOCALS~1\Temp\ccW0baaa.o(.text+0x13b)://c/docume~1/jimmyl~1/desktop/bigfol~1/testin~1/blobs.cpp: undefined reference to `grid::isEmpty(void)'
C:\DOCUME~1\JIMMYL~1\LOCALS~1\Temp\ccW0baaa.o(.text+0x15b)://c/docume~1/jimmyl~1/desktop/bigfol~1/testin~1/blobs.cpp: undefined reference to `grid::getMaxBlob(void)'

any help??

even if it linked properly

it just straight "press any key to continue"

it didn't c-cout anything

Edited by elli
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.