当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Function Definition not recognized

问题描述:

#include <vector>

#include <iostream>

using namespace std;

typedef vector<bool> row;

typedef vector<row> matrix;

//Corner functions

int population_tlc(int x, int y, matrix& grid)

{

int popu;

for(int yi = y+1; yi >= y; y--)

{

for(int xi = x+1; xi >= x; x--)

{

if (grid[yi][xi] == true)

{

popu++;

}

}

}

return popu;

}

int population_trc(int x, int y, matrix& grid)

{

int popu;

for(int yi = y+1; yi >= y; y--)

{

for(int xi = x-1; xi <= x; x++)

{

if (grid[yi][xi] == true)

{

popu++;

}

}

}

return popu;

}

int population_blc(int x, int y, matrix& grid)

{

int popu;

for(int yi = y-1; yi <= y; y++)

{

for(int xi = x-1; xi >= x; x--)

{

if (grid[yi][xi] == true)

{

popu++;

}

}

}

return popu;

}

int population_brc(int x, int y, matrix& grid)

{

int popu;

for(int yi = y-1; yi <= y; y++)

{

for(int xi = x-1; xi <= x; x++)

{

if (grid[yi][xi] == true)

{

popu++;

}

}

}

return popu;

}

//Edge functions

int population_left(int x, int y, matrix& grid)

{

int popu;

for(int yi = y-1; yi <= y+1; y++)

{

for(int xi = x; xi<= x+1; x++)

{

if(xi == x && yi == y)

{

continue;

}

if (grid[yi][xi] == true)

{

popu++;

}

}

return popu;

}

int population_top(int x, int y, matrix& grid)

{

int popu;

for(int yi = y; yi <= y+1; y++)

{

for(int xi = x-1; xi<= x+1; x++)

{

if(xi == x && yi == y)

{

continue;

}

if (grid[yi][xi] == true)

{

popu++;

}

}

}

return popu;

}

int population_right(int x, int y, matrix& grid)

{

int popu;

for(int yi = y-1; yi <= y+1; y++)

{

for(int xi = x-1; xi<= x; x++)

{

if(xi == x && yi == y)

{

continue;

}

if (grid[yi][xi] == true)

{

popu++;

}

}

}

return popu;

}

I'm trying to make a program that imitates conway's game of life. These functions take x and y coordinates as well as a reference of the grid in main. The functions check the surroundings of the x and y coordinate point in the grid and decide whether it should be true(alive) or false(dead). The problem is that in the last two functions, the IDE keeps prompting me for a ; even though im defining the function. I also have a header guard full of all the function prototypes.

#include <vector>

#include <iostream>

using namespace std;

typedef vector<bool> row;

typedef vector<row> matrix;

int population_tlc(int x, int y, matrix& grid);

int population_trc(int x, int y, matrix& grid);

int population_blc(int x, int y, matrix& grid);

int population_brc(int x, int y, matrix& grid);

int population_left(int x, int y, matrix& grid);

int population_top(int x, int y, matrix& grid);

int population_right(int x, int y, matrix& grid);

int population_bottom(int x, int y, matrix& grid);

My question is how to do I get it to "realize" that it is a function definition and an incomplete function prototype. Thank you in advance

网友答案:

There's a missing } in this function:

int population_left(int x, int y, matrix& grid)
{
    int popu;
    for(int yi = y-1; yi <= y+1; y++)
    {
        for(int xi = x; xi<= x+1; x++)
        {
            if(xi == x && yi == y)
            {
                continue;
            }
            if (grid[yi][xi] == true)
           {
               popu++;
           }
    }
    return popu;
}

So replace it by:

int population_left(int x, int y, matrix& grid)
{
    int popu;
    for(int yi = y-1; yi <= y+1; y++)
    {
        for(int xi = x; xi<= x+1; x++)
        {
            if(xi == x && yi == y)
            {
                continue;
            }
            if (grid[yi][xi] == true)
            {
               popu++;
            }
         }
    }
    return popu;
}

You can find some editors or IDE that may highlight this kind of typo for you ;) (emacs for instance)

分享给朋友:
您可能感兴趣的文章:
随机阅读: