Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Cross Reference I think  (Read 2160 times)

0 Members and 1 Guest are viewing this topic.

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Cross Reference I think
« on: February 11, 2017, 04:01:14 pm »
Quote
error C2065: 'Block' : undeclared identifier   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   10   1   Pong
error C2923: 'std::vector' : 'Block' is not a valid template type argument for parameter '_Ty'   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   10   1   Pong
error C2065: 'Block' : undeclared identifier   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   17   1   Pong
error C2923: 'std::vector' : 'Block' is not a valid template type argument for parameter '_Ty'   c:\users\dylan\documents\visual studio 2013\projects\pong\containerofblocks.h   17   1   Pong

Hey guys, these are my errors. I am not quite sure what code to include. So I will include anything linked the containerOfBlocks. The problem arose when I
#include "containterOfBlocks.h"
inside the collisionHandler class I made.

collisionHandler.h
#include "Stdafx.h"
#ifndef COLISSION_HANDLER
#define COLISSION_HANDLER

class ColisionHandler
{
public:
        ColisionHandler();
        ~ColisionHandler();

        void hasBallHitSides(Ball& ball, int windowWidth);
        void hasBallHitTop(Ball& ball);
        void hasBallHitPaddle(Ball& ball, Paddle& paddle);

        void hasPaddleHitSides(Paddle& paddle, int windowWidth);
        void HasBallHitRow(Ball& ball, ContainerOfBlocks& blockContainer, SoundHandler& soundHandler);
};

#endif

collisionHandler.cpp
#include "Stdafx.h"
#include "containerOfBlocks.h"
#include "ball.h"
#include "paddle.h"
#include "soundHandler.h"
#include "colisionHandler.h"

ColisionHandler::ColisionHandler()
{
}

ColisionHandler::~ColisionHandler()
{
}

block.cpp
#include "Stdafx.h"
#include "block.h"

Block::Block(float startX, float startY)
{
        position.x = startX;
        position.y = startY;

        colour = sf::Color::White;

        block.setSize(sf::Vector2f(width, height));
        block.setFillColor(colour);
}

Block::~Block()
{
}

sf::RectangleShape Block::getBlock()
{
        return block;
}

sf::FloatRect Block::getPosition()
{
        return block.getGlobalBounds();
}

void Block::draw(sf::RenderWindow& window)
{
        window.draw(block);
}

void Block::setPosition(int yPosition)
{
        position.y = yPosition;

        block.setPosition(position);
}

void Block::setColour(sf::Color tempColour)
{
        colour = tempColour;

        block.setFillColor(colour);
}

containerOfBlocks.h
#ifndef CONTAINER_OF_BLOCKS
#define CONTAINER_OF_BLOCKS

class ContainerOfBlocks
{
public:
        ContainerOfBlocks(int yPosition, sf::Color colour);
        ~ContainerOfBlocks();

        std::vector<Block>& getContainer();
        size_t getSize();
       
        void drawContainer(sf::RenderWindow& window);
        void deleteObjectFromVector(int objectNumber);

private:
        std::vector<Block> blockContainer;
};

#endif

containerOfBlocks.cpp
#include "Stdafx.h"
#include "block.h"
#include "containerOfBlocks.h"

ContainerOfBlocks::ContainerOfBlocks(int yPosition, sf::Color colour)
{
        blockContainer.push_back(Block(2, 2));
        blockContainer.push_back(Block(104, 2));
        blockContainer.push_back(Block(206, 2));
        blockContainer.push_back(Block(308, 2));
        blockContainer.push_back(Block(410, 2));
        blockContainer.push_back(Block(512, 2));
        blockContainer.push_back(Block(614, 2));
        blockContainer.push_back(Block(716, 2));
        blockContainer.push_back(Block(818, 2));
        blockContainer.push_back(Block(920, 2));

        for (unsigned int i = 0; i < 10; ++i)
        {
                blockContainer[i].setPosition(yPosition);
        }

        for (unsigned int i = 0; i < 10; ++i)
        {
                blockContainer[i].setColour(colour);
        }
}

ContainerOfBlocks::~ContainerOfBlocks()
{
}

std::vector<Block>& ContainerOfBlocks::getContainer()
{
        return blockContainer;
}

void ContainerOfBlocks::drawContainer(sf::RenderWindow& window)
{
        for (unsigned int i = 0; i < blockContainer.size(); ++i)
        {
                blockContainer[i].draw(window);
        }
}

void ContainerOfBlocks::deleteObjectFromVector(int objectNumber)
{
        blockContainer.erase(blockContainer.begin() + objectNumber);
}

size_t ContainerOfBlocks::getSize()
{
        return blockContainer.size();
}

I know this is a lot of code guys. But I appreciate any sort of help :) sorry I do not have a clue what has gone on!

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Cross Reference I think
« Reply #1 on: February 11, 2017, 05:22:28 pm »
Okay, I included "block.h" in containerOfBlocks.h but i don't see why this fixed my problem. Can someone explain?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Cross Reference I think
« Reply #2 on: February 11, 2017, 05:26:54 pm »
Quote
Okay, I included "block.h" in containerOfBlocks.h but i don't see why this fixed my problem. Can someone explain?
You try to use something called "Block" in containerOfBlocks.h. The compiler has no idea what "Block" is and complains. Now, you include block.h, which defines what "Block" is, in containerOfBlocks.h and the compiler is happy. You really don't understand why that fixed the problem? ;)
Laurent Gomila - SFML developer

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Cross Reference I think
« Reply #3 on: February 11, 2017, 08:01:20 pm »
Quote
You try to use something called "Block" in containerOfBlocks.h. The compiler has no idea what "Block" is and complains. Now, you include block.h, which defines what "Block" is, in containerOfBlocks.h and the compiler is happy. You really don't understand why that fixed the problem? ;)

No I don't because I thought it was bad practice to include .h file inside a .h file. The block.h was included before containerOfBlocks.h so I thought the compiler would understand?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Cross Reference I think
« Reply #4 on: February 11, 2017, 08:22:51 pm »
The block.h was included before containerOfBlocks.h so I thought the compiler would understand?
Before where?
containerOfBlocks.cpp is compiled separately and has no idea what other sources are including, only what itself includes, which is why you include its own header...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Cross Reference I think
« Reply #5 on: February 11, 2017, 08:48:30 pm »
Quote
No I don't because I thought it was bad practice to include .h file inside a .h file
Wrong. The rule is simple: a file should always include what it needs. If you write "Block" in containerOfBlocks.h, and "Block" is defined in block.h, then include block.h there.

Quote
The block.h was included before containerOfBlocks.h so I thought the compiler would understand?
Never do that. Assuming that xxx.h knows yyy.h because it's already included by zzz.h is the best way to have problems later.
Laurent Gomila - SFML developer

DylanMorgan

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Cross Reference I think
« Reply #6 on: February 12, 2017, 05:20:50 pm »
Okay thanks guys!