不命名构造函数和析构函数上的类型错误
Does not name a type error on constructor and destructor
我试图理解为什么这个类要求构造函数和析构函数的类型,从我所看到的情况来看,我的类声明似乎有问题,但是,似乎我写得对。同样重载运算符<<似乎只是返回一个错误,因为它也无法识别。
董事会.h
#ifndef BOARD_H_
#define BOARD_H_
#include <string>
#include <iostream>
#include "Board.cpp"
class Board{
public:
Board(std::string p1_token, std::string p2_token, std::string blank_token);
~Board();
void clear();
//Accessors
int numRows();
int numColumns();
int numTokensInColumn(int colNum);
// bool is true if player 1 turn, false if player 2
// Returns blank if no one wins, winner token if some wins
std::string insert(int column, bool turn);
//ostream& operator<<(ostream& os, const Board& bd);
private:
std::string p1_token;
std::string p2_token;
std::string blank_token;
std::string ** ptr_columns;
int amt_cols;
int amt_rows;
};
#endif
董事会.cpp
#include "Board.h"
#include <iostream>
#include <string>
Board::Board(std::string p1, std::string p2, std::string blank){
p1_token = p1;
p2_token = p2;
blank_token = blank;
amt_cols = 4;
amt_rows = 5;
ptr_columns = new std::string*[amt_cols];
//Right now all these are uniform
//Definition of order
//left-right first order of columns 0-end(4-1)
//Then
//down-up for each index of column ^, 0-end(5-1)
for(int I=0; I<amt_cols-1; ++I){
ptr_columns[I] = new std::string[5];
for(int V=0; V<amt_rows-1; ++V){
ptr_columns[I][V] = blank_token;
}
}
}
Board::~Board(){
delete [] ptr_columns;
}
ostream& Board::operator<<(ostream& os, const Board& bd){
for(int V = amt_rows-1; V>=0; --V){
for(int I = 0; I<amt_cols-1;++I){
os << bd.ptr_columns[I][V] << " ";
}
os << "n";
}
return os;
}
错误
Board.cpp:5:1: error: ‘Board’ does not name a type
Board::Board(std::string p1, std::string p2, std::string blank){
^~~~~
Board.cpp:25:1: error: ‘Board’ does not name a type
Board::~Board(){
^~~~~
Board.cpp:29:1: error: ‘ostream’ does not name a type
ostream& Board::operator<<(ostream& os, const Board& bd){
^~~~~~~
In file included from Board.cpp:1:0:
Board.h:19:2: error: ‘ostream’ does not name a type
ostream& operator<<(ostream& os, const Board& bd);
^~~~~~~
Board.cpp:29:1: error: ‘ostream’ does not name a type
ostream& Board::operator<<(ostream& os, const Board& bd){
^~~~~~~
In file included from Board.h:5:0,
from connect_four_main.cpp:3:
Board.cpp:5:1: error: ‘Board’ does not name a type
Board::Board(std::string p1, std::string p2, std::string blank){
^~~~~
Board.cpp:25:1: error: ‘Board’ does not name a type
Board::~Board(){
^~~~~
Board.cpp:29:1: error: ‘ostream’ does not name a type
ostream& Board::operator<<(ostream& os, const Board& bd){
^~~~~~~
In file included from connect_four_main.cpp:3:0:
Board.h:19:2: error: ‘ostream’ does not name a type
ostream& operator<<(ostream& os, const Board& bd);
您有很多问题。
不要#include
.cpp文件。
您的<< operator
应该声明为自由函数而不是成员函数,而是将其声明为friend
:
class Board{
...
friend std::ostream& operator<<(std::ostream& os, const Board& bd);
...
}
您的运算符使用来自this
而不是bd
的一些成员变量,正确的实现是:
std::ostream& operator<<(std::ostream& os, const Board& bd){
for(int V = bd.amt_rows-1; V>=0; --V){
for(int I = 0; I<bd.amt_cols-1;++I){
os << bd.ptr_columns[I][V] << " ";
}
os << "n";
}
return os;
}
相关文章:
- 类没有命名C++代码中的类型错误
- 不完整类型错误(E0409、E0070、E0515)
- 使用具有结构不完整类型错误的模板
- 不命名构造函数和析构函数上的类型错误
- 返回派生类型时出现协变类型错误
- 为什么此构造函数没有给出不完整的类型错误?
- 定义模板化结构的特征时出现不完整的类型错误
- 使用重载构造函数时出现不完整的类型错误
- 为什么我得到表达式必须有类类型错误?
- 输出 [left]=input[i] 行中的 c++ 代码中存在无效的类型错误
- 枚举成员不是类型错误
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 外部 "C" Visual Studio 2015 中的显式类型错误,DLL 测试代码
- 非默认析构函数会导致不完整的类型错误
- 为什么按引用传入会导致绑定引用类型错误
- Objective-C C++ 包装器 类型错误的不完整定义
- 输入类型错误
- 当构造函数的参数类型错误时引发异常
- 命名空间中的'bad_cast' 'std' 未命名类型错误
- C UNWORKOWN类型错误