0

I am getting an error that breed is not declared in scope even though it is right below the fucntion. What is wrong?

The exact error is: prog.cpp: In function ‘void best(std::string*)’: prog.cpp:131:15: error: ‘breed’ was not declared in this scope breed(bestRats)

#include <iostream>
#include <sstream>
#include <vector>
#include <math.h>
#include <map>
#include <string.h>
#include <queue>
#include <regex>
#include <bitset>
#include <stdlib.h>
#include <climits>
// #include "dungeonrats.cpp"

using namespace std;

/*
Finds the maximum integer in an array of integers.
Size is the size of the given array.
*/
int getMax(int* numbers, int size) {
   int maximum = INT_MIN;
   for (int i = 0; i < size; i++) {
      if (numbers[i] > maximum) maximum = numbers[i];
   }
   return maximum;
}

int getMaxi(int* numbers, int size) {
   int maximum = INT_MIN;
   int maxi;
   for (int i = 0; i < size; i++) {
      if (numbers[i] > maximum) {
         maximum = numbers[i];
         maxi = i;
      }
   }
   return maxi;
}


/*
"randomly" generates a new maze (but is it REALLY random?)
aim is 65% empty tiles
10% food tiles
10% obstacles
15% pits
*/
string getNewMaze(string mapseed) {
   string maze = mapseed.substr(6); // get everything except the 25:25:
   for (int i = 0; i < 626; i++) {
      int percentile = rand() % 100; // from 0 to 99
      if (percentile < 65) {
         maze[i] = '.';
      }
      else if (percentile >= 65 && percentile < 75) {
         maze[i] = '$';
      }
      else if (percentile >= 75 && percentile < 85) {
         maze[i] = '*';
      }
      else maze[i] = 'X';

   }



   return maze;
}

/*
A function used to print how many of each kind of tile
exist in the maze.
*/
void testFrequency(string mapseed) {
   int numEmpty = 0;
   int numFood = 0;
   int numObs = 0;
   int numPit = 0;
   for (int i = 0; i < 626; i++) {
      if (mapseed[i] == '.') {
         numEmpty++;
      }
      if (mapseed[i] == '$') {
         numFood++;
      }
      if (mapseed[i] == '*') {
         numObs++;
      }
      if (mapseed[i] == 'X') {
         numPit++;
      }
   }
   cout << "Number of empty tiles is " << numEmpty << endl;
   cout << "Number of food tiles is " << numFood << endl;
   cout << "Number of obstacles is " << numObs << endl;
   cout << "Number of pits is " << numPit << endl;


}

/*
Returns an array of size 2 containing the best two rats.
*/
void best(string r[]) {
   // r is the array of five rat genomes
   //int moves = simulator(mapseed, genome, start_row, start_col);
   int* ratMoves = new int[5]; // array that stores how long each rat lasted
   int maxIndex = 0;
   int max = INT_MIN;
   string originalMapseed = "25:25:..$.$.X.............X....$X.X*..X$..X...*X$..$...X$.$......X.$.X...XX.$.X*.*.*..X..X.**.......X..$$$...........XX.....................$...X...*.$..X..$X..........$.*..X.....$.X..$*.$X......$...X.*X$......$.**.X.X..XX$X..*....*..X.X....$...X...X........$.X....$...*...X$*........X..$*$$......$$...$*..X.$.$......$.$.$...$..X.*.....X..$......$.XX*..X.$.X......X$*.**.....X*...$..XX..X.....$....X....X...X....X.$X$..X..........$...*.X$..X...$*...........*....XXX$$.$.$..*$XX..XX..*.....$......X.XX$..$$..X$.XX.$$..X.*..*......X......$..$.$$..*...X.........$X....$X.$$.*.$.$.$..**.....X.$.$X.*.$.........$**..X.X.X$X.$.*X.X*..$*.";

   for (int i = 0; i < 5; i++) {
      string mapseed = getNewMaze(originalMapseed);
      int sumTurns = 0;
      for (int k = 0; k < 10; k++) {
         string mapseed = getNewMaze(originalMapseed);
         // sumTurns += simulator(mapseed,r[i],12,12); // uncomment this line once in correct file
      }
      ratMoves[i] = sumTurns / 10; // average of rat's performance
                                   // over 10 random maps
      if (ratMoves[i] > max) { // find the best
         max = ratMoves[i];
         maxIndex = i;
      }

   }
   string bestRats[2];
   bestRats[0] = r[maxIndex]; // store the best
   ratMoves[maxIndex] = INT_MIN; // remove the best from array of rat moves
   bestRats[1] = r[getMaxi(ratMoves, 5)]; // get the second best & store it
   breed(bestRats);

}

void breed(string r[]) {
   int cap = 10;
   string c[5];
   for (int j = 0; j<5; j++)
      for (int i = 0; i<190; i++) {
         int check = rand() % cap + 1;
         if (check % 2 == 0) {
            if (check == 0)
               c[j] += rand() % 81 + 42;
            else
               c[j] += r[1][i];
         }
         else
            c[j] += r[0][i];
      }
   best(c);
}
int main() {
   string c[5];
   for (int j = 0; j<5; j++)
      for (int i = 0; i<190; i++)
         c[j] += rand() % 81 + 42;
   best(c);
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211

3 Answers3

2

Declare

void breed(string r[]);

on the top of file before first usage or include appropriate header file containing this declaration.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • thank you, I knew it had to be something stupid – Devon Gonzalez Nov 20 '14 at 07:27
  • Yeah in C it's better to declare the prototypes of the functions before declaring it. as it's a sequential reading of the file, the compiler will be aware of the existence of it and it will avoid some of these errors. To me it's a best practice ;) – Alex Nov 20 '14 at 08:09
0

c++ checks for function names at the moment it reads them in the code (symbolically). if it hasn't read it yet, it doesn't know it exists. that's why you at least need to declare a function's prototype before using it in your code.

ergo, here you need to at least put

void breed(string r[]);

before using the function.

Jules G.M.
  • 3,624
  • 1
  • 21
  • 35
0

When the compiler processes your code, it starts at the top and goes to the end. At any given point in this process, it only "knows" about things it's already seen. So when you try to call the breed function from the best function, it doesn't know what you're talking about because it hasn't seen anything called breed yet.

To fix this, you need to let the compiler know what the breed function is before you attempt to use it. There are a few ways to do this. The easiest would be to move the entire breed function above the best function in your source file. An alternate would be to declare the breed function higher up in the file before defining it later. To declare it, simply include the line void breed (string r[]); somewhere (don't forget the ; at the end). This way the compiler knows that "breed" refers to a function that takes an array of strings and returns nothing, which is enough for it to compile calls to the function.

Gnagn
  • 151
  • 6