不命名构造函数和析构函数上的类型错误

Does not name a type error on constructor and destructor

本文关键字:类型 错误 析构函数 构造函数      更新时间:2023-10-16

我试图理解为什么这个类要求构造函数和析构函数的类型,从我所看到的情况来看,我的类声明似乎有问题,但是,似乎我写得对。同样重载运算符<<似乎只是返回一个错误,因为它也无法识别。

董事会.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;
}