简体   繁体   中英

Functions definitions outside the structure c++

I am writing a program with structs in VS13. And I want to split function declarations from function definitions. I saw next principle of "scoping" numerous times in books:

<structure name>::<method name>

But when I am trying to implement that in my program, I always get an error of impossibility of redeclaration of functions outside their class:

#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();
}

Could you say, where I am mistaken? In my e-book I see identical syntax

Your problem is you're defining your functions within main() :

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

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

Simply move all of that outside of main() :

struct game { .. };
void game::numcreate() { .. }

int main() {
    // now use game
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM