结构 c++ 之外的函数定义
Functions definitions outside the structure c++
我正在用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();
}
你能说,我错在哪里吗?在我的电子书中,我看到了相同的语法
你的问题是你在main()
中定义你的函数:
int main() {
struct game {
...
};
void game::create() { .. }
}
只需将所有这些移到main()
之外:
struct game { .. };
void game::numcreate() { .. }
int main() {
// now use game
}
相关文章:
- 不同翻译单元中不可重载的非内联函数定义
- Visual Studio中的函数声明和函数定义问题
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 具有enable_if外部类原型的模板类构造函数定义
- 类的前向声明之后的类成员函数定义,在类声明之前
- 为函数定义符号不明确的指针参数
- C++模板专用化 - 无法匹配函数定义
- 错误:在第 6 行'{'标记之前,此处不允许使用函数定义
- 找不到 #define 的函数定义
- 根据类型特征更改函数定义?
- 将抽象基类中的所有纯虚函数定义为 varaidaic 模板
- 命名空间更改函数定义
- "Type&"与C++函数定义中的"Type*"
- C++:为什么允许在另一个函数中声明函数,而不允许在函数定义中声明?
- 如何从 C++ 中的现有模板函数定义新函数
- 私有在函数定义/实现的返回值范围内是什么意思 (c++)?
- 越界成员函数定义是否需要一个完全限定的类名,直到全局范围
- 为什么c++允许成员函数定义中实例的私有成员访问
- Qt基类函数定义
- C++函数定义中参数列表后面额外一对括号的含义