help with overloaded constructors

This is a discussion on help with overloaded constructors within the C++ Programming forums, part of the General Programming Boards category; hello, i am having trouble with constructing an item in an item_node and using a pointer to move through a ...

  1. #1
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87

    help with overloaded constructors

    hello, i am having trouble with constructing an item in an item_node and using a pointer to move through a list.
    here is the relevant code:

    item class
    Code:
    class item
    {
        public:
            item(const item& other) {power=other.power; price=other.price;  
            value_to_change=other.value_to_change;};
            void use_item() {*value_to_change+=power;};
            item(int a, int b, int &c) {power = a; price = b; value_to_change = &c;};
            ~item();
            int get_price() {return price;};
            int get_power() {return power;};
               private:
            int power;
            int price;
            int *value_to_change;
    };
    the item_node class
    Code:
    class item_node
    {
        public:
            item_node(int a, int b, int &c, string input_name);
            ~item_node();
            item_node *up;
            item_node *down;
            void use_item_from_node() {item_from_node.use_item();};
            int power_from_node() {return item_from_node.get_power();};
            int price_from_node() {return item_from_node.get_price();};
            string get_name() {return name;}
        private:
        item item_from_node;
        string name;
    
    };
    Code:
    item_node::item_node(int a, int b, int &c, string input_name)
    {
        //i want to make the item_from_node to initialize here with arguments
        // (a, b, c) but cant figure out how to do it
        name = input_name;
    };
    and my sequence
    Code:
    void inventory::show_all_items()
    {
        active = ⊤ //top is the first item in the sequence
        int total_items = 0;
        for(; active != ⊥ total_items++) //bottom is the last item in the stack
        {
            cout << total_items << ": " << *active.get_name() << endl;
            active = *active.down;
        }
        return;
    }
    i need to initialize my item along with the item node but dont know how,
    i also need to know what is wrong with my pointer "active"
    i dont want to use
    Code:
     item_node : public item
    because then it would be simpler to just make item have a *next and a *last.
    also, if i overload operator= then would it work to construct it in my item node constructor like this?
    Code:
     item_from_node = item x(a, b, c)
    thanks for any help
    Last edited by bobknows; 03-11-2011 at 06:09 PM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,214
    O_o

    See "base class and member variable initialization"...

    Also, shouldn't the name of a thing be associated with the thing and not the handle to a thing?

    Soma

  3. #3
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    i am using a class called inventory to move through the nodes and i put the name in the node because then i only had to use one . to get to it. i guess you are right. i must fo had a total brain fart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  3. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 01:14 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM
  5. using overloaded constructors
    By Alicia in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 03:36 PM

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