Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法分离标题类。我得到;不为类型命名";_C++_Header_Include - Fatal编程技术网

C++ 无法分离标题类。我得到;不为类型命名";

C++ 无法分离标题类。我得到;不为类型命名";,c++,header,include,C++,Header,Include,我创建了一个包含标题、cpp和主类的类。这样做一切都是好的!当分离一个类时,我将有两个类(header+cpp)和一个主类a(Board),即使我插入了include,也不能识别类B(非法协调异常)。这可能是一个新手的问题,我可能会失去一些分数,但我一直在寻找我的问题 以下是我的工作代码(仅限于重要部分): main.cpp #include "Board.h" #include <iostream> using namespace std; int main() {

我创建了一个包含标题、cpp和主类的类。这样做一切都是好的!当分离一个类时,我将有两个类(header+cpp)和一个主类a(Board),即使我插入了include,也不能识别类B(非法协调异常)。这可能是一个新手的问题,我可能会失去一些分数,但我一直在寻找我的问题

以下是我的工作代码(仅限于重要部分):

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;
}
#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);
    }
}
我得到:

Board.cpp:在成员函数“xo&Board::operator”中: Board.cpp:60:43:错误:未声明“非法协调例外” 在这个范围内 抛出非法协调异常(c)


这怎么可能?我的意思是我把它包括在董事会中。所以Board.cpp应该承认它!?我还试图将其包含在Board.cpp中,并在Board.cpp中进行转发声明,但两者都很节省。

您的两个头文件都有
\ifndef CIRC\u H
/
\35; define CIRC\u H

因此,当包含第一个文件时(无论顺序如何),它定义了
CIRC\u H
,当包含第二个文件时,它将被忽略,因为整个文件都在
\ifndef CIRC\u H


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

\ifndef CIRC\u H
-您计划使用该fencepost多少次。为您的异常头尝试其他方法怎么样。mk.@WhozCraig你是对的,我自动使用它,因此我没有注意到它。我稍后会检查这是否是问题所在,但可能是
#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);
    }
}
#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
#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