【发布时间】:2018-05-10 23:44:08
【问题描述】:
我创建了一个带有标题、cpp 和主类的类。当这样做时,一切都很好!当分离一个类时,我将有 2 个类(header+cpp)和一个主类 A(Board)不识别类 B(IllegalCoordinateException),即使我插入了包含。这可能是一个新手问题,我可能会失去一些分数,但我一直在寻找我的问题。
这是我的工作代码(只精简到重要部分):
main.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;
}
Board.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
Board.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);
}
}
分离后:
main.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:错误:未声明“IllegalCoordinateException” 在这个范围内 throw IllegalCoordinateException(c);
这怎么可能?我的意思是我将它包含在 Board.h 中,所以 Board.cpp 应该能够识别它!?我还尝试将其包含在 Board.cpp 中,并在 Board.cpp 中进行了前向声明,但两者都很节俭。
【问题讨论】:
-
#ifndef CIRC_H- 您计划使用该围栏柱多少次。为您的异常标头尝试其他东西怎么样。马克。 -
@WhozCraig 你是对的,我自动使用它,因此我没有注意到它。我稍后会检查这是否是问题,但可能是