How to call a part of a structure within structure index?

Okay so, I wanted to see if my strncpy function worked correctly but I can't check it since the compiler is giving me an error that says:
 
cout<<inv.items[0]; 

"no match for 'operator<<' in 'std::cout << inv.invType::items[0]' "
Then it goes on saying "note" and then a bunch of gibberish I don't know how to read."Candidates are: [list candidates]"

This is what I was doing. I was using a structure
1
2
3
4
5
6
7
8
9
10
11
struct itemType{
char  ID[8];
char ProductName[20];
char Producers[20];
int Quantity;    
           };
struct invType{
itemType items[100];
int total;};
invType inv;
itemType items;


and I wanted to print out inv.items[0]
to see if I copied the last expression correctly .

Anyone know what the error means? And how to fix it?


Last edited on
You're trying to print an itemType. cout doesn't know how to print that. You have to give it something it knows how to print. In this case, that is likely the productName (which is a string -- something cout will have no problems with):

 
cout << inv.items[0].ProductName;
Last edited on
Oh, so it's not possible to print out a whole structure at once? I was attempting to print out the results of the whole item structure but is the only way you can do it by individually calling it?
You can write the whole structure at once but you need to define how it is done

write this function

1
2
3
4
5
6
7
8
ostream& operator<<(ostream& stream,const itemType& item)
{
    stream << item.ID << endl;

    etc.

    return stream;
}


when you have done that you can use the << operator to output the whole structure (in the way you have defined)
Topic archived. No new replies allowed.