How do i get it to cout what the pointer points to and not the pointer address?

int main()
{

  Item* items[10];

	items[0] = new Item(5);
	items[1] = new Item(3);
	items[2] = new Item(4);
	items[3] = new Item(9);
	items[4] = new Item(2);
	items[5] = new Item(0);
	items[6] = new Item(6);
	items[7] = new Item(1);
	items[8] = new Item(8);
	items[9] = new Item(7);
int i;
	for (i=0;i<9;i++)
		cout << items*[i];
}

Recommended Answers

All 13 Replies

Post some code which shows the Item class please.

Yes it does :)

Ok, I made two adjustments: first, since i don't know what Item is I typedef'ed it.
And second, you for loop won't print items[9], so I changed that too:

#include <iostream>

using namespace std;

int main()
{
    typedef int Item;

    Item* items[10];

    items[0] = new Item(5);
    items[1] = new Item(3);
    items[2] = new Item(4);
    items[3] = new Item(9);
    items[4] = new Item(2);
    items[5] = new Item(0);
    items[6] = new Item(6);
    items[7] = new Item(1);
    items[8] = new Item(8);
    items[9] = new Item(7);
    int i;
    for (i=0;i<10;i++)
        cout << *items[i]<<endl;
    
    cin.get();
    return 0;
}

So, as williamhemsworth says, the problem is probably inside Item class

Post some code which shows the Item class please.

class Item
{
private:
	
	int node;											
	
public:

	Item();
	Item(int);
	
	~Item();
				
};






#include "Item.h"

Item::Item()
{
	node = 0;
};
Item::Item(int n)
{
	node = n;
};

You need to overload operator <<.

The problem isn't in pointers, they work fine.
But when you write: cout << *items[i] it's like you wrote:

Item a(19);
cout<<a;

And that isn't defined

You need to overload operator <<.

The problem isn't in pointers, they work fine.
But when you write: cout << *items[i] it's like you wrote:

Item a(19);
cout<<a;

And that isn't defined

so how would I do that?

Write a function:

std::ostream& operator<<(std::ostream& os, Item const& It);

And declare it as a friend of class.

Inside function you need to choose what to print.
For example:
os << some_class_variable << some_other_class_variable;
or similar.

And try to google it out, you have lots of examples of overloading <<

Yes it does :)

Ok, I made two adjustments: first, since i don't know what Item is I typedef'ed it.
And second, you for loop won't print items[9], so I changed that too:

#include <iostream>

using namespace std;

int main()
{
    typedef int Item;

    Item* items[10];

    items[0] = new Item(5);
    items[1] = new Item(3);
    items[2] = new Item(4);
    items[3] = new Item(9);
    items[4] = new Item(2);
    items[5] = new Item(0);
    items[6] = new Item(6);
    items[7] = new Item(1);
    items[8] = new Item(8);
    items[9] = new Item(7);
    int i;
    for (i=0;i<10;i++)
        cout << *items[i]<<endl;
    
    cin.get();
    return 0;
}

So, as williamhemsworth says, the problem is probably inside Item class

what is cin.get()

Oh, nevermind that.
I have to add that because my command prompt would close immediately after the execution of program (so I wouldn't be able to see what the program has printed out)

Oh, nevermind that.
I have to add that because my command prompt would close immediately after the execution of program (so I wouldn't be able to see what the program has printed out)

ok

Another way to do this.

#include <iostream>
using namespace std;

class Item {
private:
   int node;											

public:
   Item();
   Item(int);
   operator int();

   ~Item();
};

Item::Item() {
   node = 0;
}

Item::Item(int n) {
   node = n;
}

Item::operator int() {
   return node;
}

int main() {
   Item* items[10];

   items[0] = new Item(5);
   items[1] = new Item(3);
   items[2] = new Item(4);
   items[3] = new Item(9);
   items[4] = new Item(2);
   items[5] = new Item(0);
   items[6] = new Item(6);
   items[7] = new Item(1);
   items[8] = new Item(8);
   items[9] = new Item(7);

   for (int i = 0; i < 10; i++) {
      cout << *items[i] << endl;
   }

   cin.ignore();
   return 0;
}

Whenever an variable (type Item) is used, it will be recognized as an int by what is returned from the Item::operator int() function.

Item *tempItem = items;
cout << *tempItem;

????

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.