无法分隔标头类。我得到"does not name a type"
unable to seperate header classes. I get "does not name a type"
我创建了一个带有标头、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
范围内。
解决方案:为每个头文件使用不同的宏名称。
相关文章:
- 将系数存储在头文件的数组中("does not name a type"错误)
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 已声明时"Does not name a type"错误
- 嵌套类"does not name a type"
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- Eigen loadMarket does not compile
- 为什么当我的代码超出函数范围时,"does not name a type"出现编译器错误?
- 创建C++ Redis 模块 - "does not export RedisModule_OnLoad() symbol"
- 无法分隔标头类。我得到"does not name a type"
- 模板元编程:"does not have integral or enumeration type"
- 如何修复类函数"prototype does not match"和"cadidate is"错误
- #define 的"Declaration does not declare anything"错误
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 在 c++ 中,如果包含标头,为什么我会收到'does not a name of type'错误?
- 什么原因导致我的"‘FatalError’ in namespace ‘Foam’ does not name a type"错误?
- C++ 迭代器错误"does not refer to a value"
- 编译错误"String does not name a type"