I am getting the following errors when making Arrays of two obecjts.. Edge and Box.
error: conversion from 'const Edge*' to non-scalar type 'Edge' requested.
I am hoping to return an array of Edges.
On this header file:
class Box
{
private:
bool playerOwned;
bool computerOwned;
Edge boxEdges[4];
int openEdges;
bool full;
public:
Box();
Box(int x, int y);
void setEdges(Edge boxTop, Edge boxBottom, Edge boxLeft, Edge boxRight);
void addEdgeToBox(Edge edge); //add edge to edgeArray.
void setPlayerOwned(bool point);
Edge getBoxEdges() const {return boxEdges;} ****//Error****
bool getPlayerOwned() const {return playerOwned;}
void setComputerOwned(bool point);
bool getComputerOwned()const {return computerOwned;}
int getOpenEdges() const {return openEdges;}
bool isFull()const {return full;}
};
std::ostream& operator<< (std::ostream& out, Box box);
I get the same error except replace ‘Edge’ with ‘Box’ on the following line in a non-header file attempting to create a Box.
Box box = new Box(x+i,y);
One error is right here. You should write this as:
It is because when you use
new, you’re allocating memory, and only a pointer can hold a memory, soboxhas to be pointer type.Similarly,
should be written as:
It is because
boxEdgesis an array, which can decay into pointer type to its first element, and since it is const member function,boxEdgeswill decay intoconst Edge*.By the way, instead of pointer in the first case, you use automatic object as:
I would suggest you to make the second parameter of
operator<<a const reference:This avoids unnecessary copy!