Thread: accessing structure issues

  1. #1
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164

    accessing structure issues

    Can anyone please show me whats going wrong here.

    I'm trying to access a structure from within another structure but am recieving errors.

    Structures are global and are as follows:
    Code:
    struct StockItem
    {
        int code;
        char desc[20];
        float price;
    }stock[NUM_OF_ITEMS];
    
    struct BillLine
    {
        StockItem stock;
        float weight;
        float cost;
    }bill_line[50];
    The 'stock' array is already populated.
    My code is as follows:
    Code:
    BillLine bill_item(void)
    {
        BillLine bill;
        int item;
        
        cls();
        cout << "Enter item number purchased: ";
        cin >> item;
        item -= 1;
    /*line 213*/    bill.stock = stock[item];
        cout << "Weight: ";
        cin >> bill.weight;
        
        bill.cost = stock[item].price * bill.weight;
        
        return (bill);
    }
    
    void print_bill ( BillLine bill_line[50], int num_bill_items )
    {
        for (int i=0; i<num_bill_items; ++i)
        {
    /*line 240*/        cout << "Name = " << bill_line[i].stock.desc << endl;
            cout << "Weight = " << bill_line[i].weight << endl;
            cout << "Cost = " << bill_line[i].cost << endl;
        }
    }
    MY compile log is as follows:

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Ged\code\assignment\best.cpp" -o "C:\Ged\code\assignment\best.exe" -g3 -O0 -g3 -I"C:\Ged\Dev-Cpp\include\c++" -I"C:\Ged\Dev-Cpp\include\c++\mingw32" -I"C:\Ged\Dev-Cpp\include\c++\backward" -I"C:\Ged\Dev-Cpp\include" -L"C:\Ged\Dev-Cpp\lib"
    C:/Ged/code/assignment/best.cpp: In function `BillLine bill_item()':
    C:/Ged/code/assignment/best.cpp:213: incompatible types in assignment of `
    StockItem' to `StockItem[125]'

    C:/Ged/code/assignment/best.cpp: In function `void print_bill(BillLine*, int)':
    C:/Ged/code/assignment/best.cpp:240: request for member `desc' in `(bill_line +
    (+(i * 3508)))->BillLine::stock', which is of non-aggregate type `
    StockItem[125]'

    Execution terminated
    Last edited by eth0; 01-12-2004 at 08:50 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about annotating your code snippet with a few line numbers, specifically, the line numbers in the error messages.

    Or is this yet another case of someone writing too much code between compile attempts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Hi Salem,

    I've commented in the 2 line numbers which are causing the problems.

    Its not a case of writing too much code. I'm writing one function at a time. The rest of the program works perfectly, its just the section I have posted that i'm having problems with.

    Thanks.

  4. #4
    Registered User eth0's Avatar
    Join Date
    Dec 2003
    Posts
    164
    Nevermind. All sorted now. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding accessing the structure member
    By kollurisrinu in forum C Programming
    Replies: 5
    Last Post: 06-18-2008, 04:51 AM
  2. Help...accessing character arrays in a structure
    By mathewmc in forum C Programming
    Replies: 7
    Last Post: 10-31-2006, 11:20 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Accessing structure members
    By foniks munkee in forum C Programming
    Replies: 18
    Last Post: 02-13-2002, 03:06 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM