未在构造函数和析构函数上命名类型错误

问题描述 投票:0回答:1

我试图理解为什么这个类在构造函数和析构函数上要求一个类型,从我所看到的看来,类声明有问题,但是似乎我写的很对。另外,重载operator <

Board.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

Board.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);
c++
1个回答
1
投票

您有很多问题。

不要#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;
}
© www.soinside.com 2019 - 2024. All rights reserved.