Assignment problem

This is a discussion on Assignment problem within the C++ Programming forums, part of the General Programming Boards category; Someone explain why this doesn't work: Code: void tile::setCoords (coord* tr, coord* br, coord* bl, coord* tl) { std::cout << ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Assignment problem

    Someone explain why this doesn't work:

    Code:
    void tile::setCoords (coord* tr, coord* br, coord* bl, coord* tl)
    {
            std::cout << "assigning [" << tr->x << ", " << tr->y << "]\n";
            std::cout << "assigning [" << br->x << ", " << br->y << "]\n";
            std::cout << "assigning [" << bl->x << ", " << bl->y << "]\n";
            std::cout << "assigning [" << tl->x << ", " << tl->y << "]\n";
    
            coords.topRight->x = tr->x;
            coords.topRight->y = tr->y;
            coords.bottomRight->x = br->x;
            coords.bottomLeft->y = br->y;
            coords.bottomLeft->x = bl->x;
            coords.bottomLeft->y = bl->y;
            coords.topLeft->x = tl->x;
            coords.topLeft->y = tl->y;
    
            std::cout << "assigned [" << coords.topRight->x << ", " << coords.topRight->y << "]\n";
            std::cout << "assigned [" << coords.bottomRight->x << ", " << coords.bottomRight->y << "]\n";
            std::cout << "assigned [" << coords.bottomLeft->x << ", " << coords.bottomLeft->y << "]\n";
            std::cout << "assigned [" << coords.topLeft->x << ", " << coords.topLeft->y << "]\n";
    }
    The answer will probably hit me in an hour - I've been doing that lately

    Either they don't get assigned properly or I'm outputting what I don't mean to - I'm pretty sure it's the former since nothing gets drawn.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    At a glance:
    Code:
    coords.bottomLeft->y = br->y;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    bottomright y is not assigned, bottomleft y is assigned twice

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Well I cleared that up, but this is still my output (along with nothing being drawn):

    Code:
    assigning [50, 0]
    assigning [50, 50]
    assigning [0, 50]
    assigning [0, 0]
    assigned [0, 0]
    assigned [0, 0]
    assigned [0, 0]
    assigned [0, 0]
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    coords.topRight->x = tr->x;
    coords.topRight->y = tr->y;
    If they're the same type (I can't see why they shouldn't be), why not just do
    *coords.topRight = *tr;

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    They are the same type but it still doesn't work. I could post my program in it's entirety so far, but tis rather long in several files.

    Oh what the hell, this is bugging me like mad:

    tile.h:
    Code:
    #pragma once
    
    typedef struct _coord {
            int     x;
            int     y;
    } coord;
    
    class tile
    {
            union _coords {
                    coord*     topRight;
                    coord*     bottomRight;
                    coord*     bottomLeft;
                    coord*     topLeft;
            } coords;
    
    public:
            tile (void);
            ~tile (void);
    
            void    draw ();
            void    setCoords (coord* tr, coord* br, coord* bl, coord* tl);
            void    dumpCoords ();
    };
    tile.cpp
    Code:
    #include "StdAfx.h"
    #include ".\tile.h"
    
    tile::tile (void)
    {
            coords.bottomLeft = new coord;
            coords.bottomRight = new coord;
            coords.topLeft = new coord;
            coords.topRight = new coord;
    }
    
    tile::~tile (void)
    {
            delete coords.bottomLeft;
            delete coords.bottomRight;
            delete coords.topLeft;
            delete coords.topRight;
    }
    
    
    // draw
    //      Draws tile @ coords
    void tile::draw ()
    {
            glBegin (GL_QUADS);
                    glVertex2i (coords.topRight->x, coords.topRight->y);
                    glVertex2i (coords.bottomRight->x, coords.bottomRight->y);
                    glVertex2i (coords.bottomLeft->x, coords.bottomLeft->y);
                    glVertex2i (coords.topLeft->x, coords.topLeft->y);
            glEnd ();
    }
    
    // setCoords
    //
    void tile::setCoords (coord* tr, coord* br, coord* bl, coord* tl)
    {
            std::cout << "assigning [" << tr->x << ", " << tr->y << "]\n";
            std::cout << "assigning [" << br->x << ", " << br->y << "]\n";
            std::cout << "assigning [" << bl->x << ", " << bl->y << "]\n";
            std::cout << "assigning [" << tl->x << ", " << tl->y << "]\n";
    
            *coords.topRight = *tr;
            *coords.bottomRight = *br;
            *coords.bottomLeft = *bl;
            *coords.topLeft = *tl;
    
            std::cout << "assigned [" << coords.topRight->x << ", " << coords.topRight->y << "]\n";
            std::cout << "assigned [" << coords.bottomRight->x << ", " << coords.bottomRight->y << "]\n";
            std::cout << "assigned [" << coords.bottomLeft->x << ", " << coords.bottomLeft->y << "]\n";
            std::cout << "assigned [" << coords.topLeft->x << ", " << coords.topLeft->y << "]\n";
    }
    
    // dumpCoords
    //      Output coords to stdout for debugging
    void tile::dumpCoords ()
    {
            std::cout << "tr: [" << coords.topRight->x << ", " << coords.topRight->y << "]\n";
            std::cout << "br: [" << coords.bottomRight->x << ", " << coords.bottomRight->y << "]\n";
            std::cout << "bl: [" << coords.bottomLeft->x << ", " << coords.bottomLeft->y << "]\n";
            std::cout << "tl: [" << coords.topLeft->x << ", " << coords.topLeft->y << "]\n";
    }
    EDIT: I take back what I said at the top because half of it isn't here - I don't think it's relevant but if you need it, just shout.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Why a union?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I naively decided I wanted the coords in the same place in memory. That is what a union does right? Right?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Well, unions were used to save memory on really weak machines. Now that they're so powerful, unions aren't needed anymore really (maybe in some cases, but I can't think of any).
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I had a niggling the union might have something to do with it; I've removed it an now tis working!

    But why?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    Quote Originally Posted by ahluka
    I naively decided I wanted the coords in the same place in memory. That is what a union does right? Right?
    Using union the coords are in exactly the same place in memory. That is, you actually have only one set of coordinates. They are all in the same memory location, and since a memory location can have only one value in it, all the coordinates are the same.

    I think you'd like to use a struct or class instead...

  12. #12
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ah now I understand them. I've decided not to use a struct and just have them as private members - no reason not to.

    For your viewing pleasure (and because I'm pleased with it):

    http://www.ahluka.co.uk/tile_screen.JPG

    The output is correct I'm just fiddling with it and have created two tiles in the same place. Don't ask.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  2. Another assignment problem
    By the_winky_files in forum C Programming
    Replies: 14
    Last Post: 10-03-2005, 10:33 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. problem on assignment
    By rodney1001 in forum C Programming
    Replies: 1
    Last Post: 10-29-2001, 02:49 PM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21