-1
#pragma once
#include "SFML\Graphics.hpp"

class Tile {

public:
    Tile();
    sf::RectangleShape getRect() const;
    sf::Vector2f getPos() const; 

    void highlightTile();
    bool isTileHighlighted() const;
    void turnOffHighlight();

    void setPos(const sf::Vector2f&);
    void setColor(const sf::Color&);

private:
    sf::RectangleShape m_tile;
    bool m_isHighlighted;
};


#include "Tile.h"

Tile::Tile() : m_tile(sf::Vector2f(0, 0)), m_isHighlighted(false) {
}


#pragma once
#include "SFML\Graphics.hpp"
#include "Windows.h"
#include "Tile.h"   
#include <iostream>

class Grid
{
public:
    Grid();
    Grid(float);
    sf::Vector2f getCoords(int, int) const;
    sf::Vector2i windowCoordsToRowCol(const sf::Vector2i&, float) const;
    bool isGamePieceOnTile(const sf::RectangleShape&) const;

    void drawBoard(Windows&);
    void update(Windows&, const sf::Event&, float);

    ~Grid();
private:
    sf::Vector2f m_gridMap[8][8];
    Tile m_tileSet[8][8];
};


Grid::Grid(float squareDim) 
{
    Tile tilePiece();
    sf::Vector2f position(0, 0);
    int counter = 0; //counter for whether the column is even or odd
    int counter1 = 0; //counter for whether we are on an even or odd row
    for (int row = 0; row < 8; row++) {
        for (int column = 0; column < 8; column++) {

            if (counter1 % 2 == 0 && counter % 2 == 0 || counter1 % 2 != 0 && counter % 2 != 0) {
                tilePiece.setColor(sf::Color::Red);
            }
            else {
                tilePiece.setColor(sf::Color::White);
            }

            tilePiece.setPosition(position);
            m_tileSet[row][column] = new tilePiece;
            m_gridMap[row][column] = sf::Vector2f(tilePiece.getPos().x + squareDim / 2, tilePiece.getPos().y + squareDim / 2);
            position.x += squareDim;
            counter++;
        }
        position.y += squareDim;
        position.x = 0;
        counter = 0;
        counter1++;
    }
}

I instantiate Tile in the Grid constructor and cannot do any tilePiece.___. it says it must have class type and that error does not make sense to me...

hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

1 Answers1

0

This is an unfortunate behaviour of C++ when invoking default (parameter-less) constructors:

Tile tilePiece();

What does the line do? You'd want to (by the usages) define a variable tilePiece of type Tile, and default initialize it. However, what this does, is it defines a function tilePiece that takes no parameters. See for example this question or this one.

A simple fix would be to omit the redundant (and confusing to the compiler) braces:

Tile tilePiece;
Community
  • 1
  • 1
hauron
  • 4,550
  • 5
  • 35
  • 52