无法分隔标头类。我得到"does not name a type"

unable to seperate header classes. I get "does not name a type"

本文关键字:does not name type 分隔      更新时间:2023-10-16

我创建了一个带有标头、cpp 和主类的类。这样做的时候一切都很好!当分离一个类时,我将有 2 个类(header+cpp(和一个主要的 A 类(板(无法识别 B 类(非法坐标异常(,即使我插入了包含。这可能是一个新手问题,我可能会失去一些分数,但我坚持找到我的问题。

这是我的工作代码(仅精简到重要部分(:

主.cpp

#include "Board.h"
#include <iostream>
using namespace std;
int main() {

Board board1{4};  // Initializes a 4x4 board
try {
board1[{3,4}]='O';   // This should raise an exception
} catch (const IllegalCoordinateException& ex) {
cout << "Illegal coordinate"  << ex.theCoordinate() << endl;  // prints "Illegal coordinate: 3,4"
}

return 0;
}

董事会.h

#ifndef CIRC_H
#define CIRC_H
#include <iostream>
#include <string>
using namespace std;
struct coord {
int x;
int y;
};

class IllegalCoordinateException{
coord _coord;
public:
IllegalCoordinateException(coord c){
_coord = c;
}
string theCoordinate() const{
return to_string(_coord.x)+","+to_string(_coord.y);
}
};

class xo{
char x;
public:
char getChar() const{return x;}
char& operator= (const char c){x = c;}
xo& operator= (const xo _xo){
x = _xo.getChar();
return *this;
}
void clear(){
x = '.';
}

operator char() const{
return x;
}
};

class Board{
private:
coord _coord;
xo** board;
int size;
public:
Board();
Board(int v);
~Board();
xo& operator[](coord c); // here is where I use "IllegalCoordinateException"
};

#endif

董事会.cpp

#include <iostream>
#include <vector>
#include "Board.h"
using namespace std;

void freeBoard(xo** board,int size){
for(int i = 0 ; i < size ; i++){
delete[] board[i];
}
}
Board::Board()
{
size = 0;
board = new xo* [size];
}
Board::Board(int v)
{
size = v;
board = new xo* [size];
for (int i=0; i<size; i++)
{
board[i] = new xo[size];
for(int j = 0 ; j < size ; j++){
board[i][j].clear();
}
}
}
Board::~Board(){
freeBoard(board,size);
delete[] board;
}

xo& Board::operator[](coord c)
{
if(c.x < size && c.y < size)
{
return board[c.x][c.y];
}
else
{
throw IllegalCoordinateException(c);
}
}

分离后:

主.cpp无差异

Board.h也 cpp 没有差异

#ifndef CIRC_H
#define CIRC_H
#include "IllegalCoordinateException.h"
#include <iostream>
#include <string>
using namespace std;

struct coord {
int x;
int y;
};

class xo{
char x;
public:
char getChar() const{return x;}
char& operator= (const char c){x = c;}
xo& operator= (const xo _xo){
x = _xo.getChar();
return *this;
}
void clear(){
x = '.';
}

operator char() const{
return x;
}
};

class Board{
private:
coord _coord;
xo** board;
int size;
public:
Board();
Board(int v);
~Board();
xo& operator[](coord c);
};

#endif

IllegalCoordinateException.h//我也在我的代码中将 .h 和 .cpp 分开(但当然没有真正的差异。

#ifndef CIRC_H
#define CIRC_H
#include <iostream>
#include "Board.h"
using namespace std;
class IllegalCoordinateException{
coord _coord;
public:
IllegalCoordinateException(coord c){ _coord = c;}
string theCoordinate() const{return to_string(_coord.x)+","+to_string(_coord.y);}
};
#endif

做的时候

$ g++ -g -Og -std=c++0x main.cpp Board.cpp IllegalCoordinateException.cpp

我得到:

Board.cpp:在成员函数'xo& Board::operator'中: Board.cpp:60:43:错误:"非法坐标异常"未声明 在此范围内 抛出非法坐标异常(c(;

这怎么可能?我的意思是我把它包括在董事会中,所以董事会.cpp应该承认它!?我也试图把它列入董事会.cpp并在董事会中做出前瞻性声明.cpp但两者都很节俭。

你的两个头文件都有#ifndef CIRC_H/#define CIRC_H

因此,当包含第一个文件(无论哪个顺序(时,它定义了CIRC_H,而当包含第二个文件时,它会被忽略,因为整个文件都在#ifndef CIRC_H范围内。

解决方案:为每个头文件使用不同的宏名称。