C++ 双重声明c++;
所以我在几个文件中有一些代码: cells.cpp:C++ 双重声明c++;,c++,oop,C++,Oop,所以我在几个文件中有一些代码: cells.cpp: #include "cells.h" #include <iostream> using namespace std; char convertIntChar (int symbolNumber) { char charR; switch (symbolNumber) { case 0: charR='0'; break; // lust of case code
#include "cells.h"
#include <iostream>
using namespace std;
char convertIntChar (int symbolNumber)
{
char charR;
switch (symbolNumber)
{
case 0:
charR='0';
break;
// lust of case code here
case 63:
charR='\\';
break;
}
return charR;
}
class cell
{
public:
int iPosition;
char chPosition;
cell ()
{
static int i = -1;
i++;
chPosition=convertIntChar (i);
iPosition=i;
cout << " " << iPosition; //two lines of code to test
cout << " " << chPosition; //constructor
}
protected:
};
我有一个错误,听起来像
//filepath\| 5 |错误:“convertIntChar”声明中有两个或多个数据类型|
||==生成完成:1个错误,0个警告(0分7秒)===|
可能是什么。很抱歉没有问题。首先,此转发声明需要分号:
class cell;
// ^
第二,您不能在这里使用转发声明main.cpp
需要查看单元格
类定义。因此,您应该将定义放在cells.h
中。例如:
单元格.h
:
#ifndef CELLS_H_INCLUDED
#define CELLS_H_INCLUDED
class cell
{
public:
int iPosition;
char chPosition;
cell ();
};
char convertIntChar(int symbolNumber);
#endif
cells.cpp
:
#include "cells.h"
#include <iostream>
char convertIntChar (int symbolNumber)
{
char charR;
// as before
return charR;
}
cell::cell ()
{
static int i = -1;
i++;
chPosition=convertIntChar (i);
iPosition=i;
std::cout << " " << iPosition; //two lines of code to test
std::cout << " " << chPosition; //constructor
}
#包括“cells.h”
#包括
字符转换器INTCHAR(int symbolNumber)
{
查尔;
//一如既往
返回字符;
}
单元::单元()
{
静态int i=-1;
i++;
chPosition=convertinchar(i);
i位置=i;
std::cout您在cpp文件中有类单元格
,它应该进入.h文件
然后在cells.h中,在类单元格
之后缺少一个;
代替cell.h中的前向声明,将类放在那里。“//这里是case code的欲望”-您需要获得更多!!!ok的副本,谢谢,但是如何从main.cpp生成cell类,它已经在cells.h中宣布了。是吗?我已经纠正了;还有两个新错误:F:\code\bytecode\main.cpp|在函数“int main()中)“:| main.cpp | 10 |错误:数组“cell cells[64]”的元素的类型不完整| main.cpp | 10 |错误:不知道“cells”的存储大小| main.cpp | 10 |警告:未使用的变量“cells”[-Wunused variable]| | | 10==构建完成:2个错误,1个警告(0分8秒)==|@user2513649正如我所说,将单元格
类定义放在cells.h
中,而不是前向声明中。@user2513649我添加了一个示例。@user2513649没问题,很高兴它有帮助。
#ifndef CELLS_H_INCLUDED
#define CELLS_H_INCLUDED
class cell
{
public:
int iPosition;
char chPosition;
cell ();
};
char convertIntChar(int symbolNumber);
#endif
#include "cells.h"
#include <iostream>
char convertIntChar (int symbolNumber)
{
char charR;
// as before
return charR;
}
cell::cell ()
{
static int i = -1;
i++;
chPosition=convertIntChar (i);
iPosition=i;
std::cout << " " << iPosition; //two lines of code to test
std::cout << " " << chPosition; //constructor
}