结构c ++之外的函数定义

时间:2015-04-18 13:25:12

标签: c++ struct

我正在用VS13中的结构编写一个程序。我想从函数定义中拆分函数声明。我看到了"范围"的下一个原则。书中多次出现:

<structure name>::<method name>

但是当我试图在我的程序中实现它时,我总是会在类外重新声明函数的错误:

#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <array>
#define com
using namespace std;

int main() {
    struct game {
        int row, col, size, count = 1;
        vector <vector<char>> b;
        enum board { blank, zero, cross };
        char num[3];
        board first, second;
        void numcreate();
        void create();
        void print();
        int check();
        void makemove();
        void process();
        void play();
    };

    void game::numcreate() {
        num[0] = '_';
        num[1] = 'o';
        num[2] = 'x';
    }
    void game::create() {
        b.resize(size);
        for (int i = 0; i < size; ++i)
        {
            b[i].resize(size);
            b[i].insert(b[i].begin(), size, num[blank]);
            copy(b[i].begin(), b[i].end(), ostream_iterator<char>(cout, " "));
            cout << endl;
        }
    }
    void game::print() {
        for (int i = 0; i < size; ++i)
        {
            copy(b[i].begin(), b[i].end(), ostream_iterator<char>(cout, " "));
            cout << endl;
        }
    }
    int game::check() {
        if (
            all_of(b[0].begin(), b[0].end(), [this](char t) {return (t == num[zero]); }) ||
            all_of(b[1].begin(), b[1].end(), [this](char t) {return (t == num[zero]); }) ||
            all_of(b[1].begin(), b[1].end(), [this](char t) {return (t == num[zero]); }) ||
            b[0][0] == num[zero] && b[1][1] == num[zero] && b[2][2] == num[zero] ||
            b[0][2] == num[zero] && b[1][1] == num[zero] && b[2][0] == num[zero] ||
            b[0][0] == num[zero] && b[1][0] == num[zero] && b[2][0] == num[zero] ||
            b[0][1] == num[zero] && b[1][1] == num[zero] && b[2][1] == num[zero] ||
            b[0][2] == num[zero] && b[1][2] == num[zero] && b[2][2] == num[zero]){
            return 1;
        }
        if (all_of(b[0].begin(), b[0].end(), [this](char t) {return (t == num[cross]); }) ||
            all_of(b[1].begin(), b[1].end(), [this](char t) {return (t == num[cross]); }) ||
            all_of(b[1].begin(), b[1].end(), [this](char t) {return (t == num[cross]); }) ||
            b[0][0] == num[cross] && b[1][1] == num[cross] && b[2][2] == num[cross] ||
            b[0][2] == num[cross] && b[1][1] == num[cross] && b[2][0] == num[cross] ||
            b[0][0] == num[cross] && b[1][0] == num[cross] && b[2][0] == num[cross] ||
            b[0][1] == num[cross] && b[1][1] == num[cross] && b[2][1] == num[cross] ||
            b[0][2] == num[cross] && b[1][2] == num[cross] && b[2][2] == num[cross]) {
            return 2;
        }
    }
    void game::makemove() {

        if (count % 2 == 0){
            cout << "Please, enter the position on the board" << endl;
            cin >> row;
            cin >> col;
            b[row][col] = num[zero];
        }
        else {
            cout << "Please, enter the position on the board" << endl;
            cin >> row;
            cin >> col;
            b[row][col] = num[cross];
        }
    }
    void game::process() {
        while (1) {
            if (check() == 1) { cout << "First player has won" << endl; break; }
            if (check() == 2) { cout << "Second player has won" << endl; break; }
            makemove();
            print();
            system("cls");
            print();
            ++count;
        }
    }

    void game::play() {
        numcreate();
        create();
        process();
    }

    game one;
    one.size = 3;
    one.play();
    cin.ignore();
    cin.get();
}

你能说,我错了吗?在我的电子书中,我看到相同的语法

1 个答案:

答案 0 :(得分:3)

您的问题是您在 main()中定义了的功能:

int main() {
    struct game {
        ...
    };

    void game::create() { .. }
}

只需移动main()

之外的所有
struct game { .. };
void game::numcreate() { .. }

int main() {
    // now use game
}