c++ - 没有在构造函数和析构函数上命名类型错误
问题描述
我试图理解为什么这个类要求构造函数和析构函数的类型,从我所见,我的类声明似乎有问题,但似乎我写对了。重载 operator<< 似乎也只是返回一个错误,因为它也无法识别。
板子.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;
}
推荐阅读
- angular - 运行 ng test 时缺少声明文件
- java - 我正在以反向或自然顺序对 java 中的日期列表进行排序,但它按年份分组
- c# - 为什么 console.writeline 在 Visual Studio for Mac 中不起作用?
- neural-network - 使用神经网络进行投资组合选择
- c# - C# 应用程序在 InitializeComponent() 启动时崩溃,抛出 CultureNotFoundException
- c++ - 将指针作为函数参数传递
- delphi - 如何在 Delphi 应用程序中检测 TPopupMenu 的 OnClose (OnPopDown) 事件
- laravel - 如何使用 laravel 在 Outlook 上创建日历事件?
- c++ - C/C++ 中的指针实际上是如何存储地址的?
- linux - 轻松交换 terraform 版本的工具