Thread: struct constructor woes

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    struct constructor woes

    I have this struct that has a constructor to initialize the values of its members. It doesn't seem to work, because when I output the values of it's members I get garbage. please tell me what is wrong with my code

    Code:
    #include <iostream>
    using namespace std;
    
    typedef unsigned char u8;
    typedef unsigned short u16;
    typedef unsigned long u32;
    typedef signed char s8;
    typedef signed short s16;
    typedef signed long s32;
    
    struct object
    {
    	s8 x, y;
    	u8 height, width, key;
    	object* next;
    	object(s8 X, s8 Y, u8 Width, u8 Height, u8 Key);
    	void report();
    };
    
    object::object(s8 X, s8 Y, u8 Width, u8 Height, u8 Key)
    {
    	x = X;
    	y = Y;
    	width = Width;
    	height = Height;
    	key = Key;
    }
    
    void object::report()
    {
    	cout << "x: " << x << endl;
    	cout << "y: " << y << endl;
    	cout << "width: " << width << endl;
    	cout << "height: " << height << endl;
    	cout << "key: " << key << endl;
    }
    
    int main()
    {
    	bool screen[240][160];
    	for(int a = 0; a < 240; a++)
    		for(int b = 0; b < 160; b++)
    			screen[a][b];
    	
    	object obstacle1(0, 0, 15, 15, 1);
    	object obstacle2(150, 100, 12, 12, 2);
    	object character(50, 50, 20, 20, 0);
    	
    	obstacle1.report();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    46
    It doesn't seem to work, because when I output the values of it's members I get garbage.
    cout determines the type of a variable you pass it and prints the appropriate representation. In this case your variables are of the type s8 and u8, which are typedef'd char types. So cout sees chars and prints chars. Assuming you use the ASCII character set, 0, 15, and 1 will give you nothing, and funny symbols. You're probably seeing this as garbage. To solve the problem, use an explicit cast to int in the report function so that you can see the numeric value.
    Code:
    void object::report()
    {
        cout << "x: " << int(x) << endl;
        cout << "y: " << int(y) << endl;
        cout << "width: " << int(width) << endl;
        cout << "height: " << int(height) << endl;
        cout << "key: " << int(key) << endl;
    }

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    Talking

    How foolish of me! Thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM