【问题标题】:unable to seperate header classes. I get "does not name a type"无法分离标头类。我得到“没有命名类型”
【发布时间】:2018-05-10 23:44:08
【问题描述】:

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

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

ma​​in.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);
    }
}

分离后:

ma​​in.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 你是对的,我自动使用它,因此我没有注意到它。我稍后会检查这是否是问题,但可能是

标签: c++ header include


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多