Can u use an array in a class program, I'm getting errors in this c...

Read below in the Message box, because the board fraggs the program all up.



Please help me find the errors in this class program, it is suppose to

// Program Parts reads stock numbers, prices, and quantities

// from file inventory, stores them in a list, and prints the list.




Thanks for any help you can give me

Brad



errors:



Error 24: Constructer cannot have a return type specification

Error 37: Call to undefined function 'Getlist'

Error 38: Call to PrintList in function main()

Error 52: Undefined symbol 'list' in ListType::ListType()

Warning 37: Structure passed by value in function main()














// from file inventory, stores them in a list, and prints the list.



#include

#include

#include



const int MAX_LENGTH = 50;



struct ItemType // declare record of items

{

int stockNumber;

float price;

int quantity;

};


class ListType // declare list class

{

private:

int length;

ItemType items[MAX_LENGTH];

public:

void ListType(); //empty list

void GetList(ifstream& myFile); //reads values

void PrintList(); //prints list

};



int main ()

{



ListType list; // define list variable

cout.setf(ios::fixed, ios::floatfield);

cout.setf(ios::showpoint);

ifstream inventory;

inventory.open("parts.dat");

GetList(inventory);

PrintList();

return 0;

}



//*******************************************


ListType::ListType()

// Post: length has been set to zero.

{

int row;



for (row = 0; row < MAX_LENGTH; row++)

{

list.items[MAX_LENGTH].stockNumber = 0;

list.items[MAX_LENGTH].price = 0;

list.items[MAX_LENGTH].quantity = 0;

}

list.length = 0;

}



//*******************************************



void ListType::GetList(ifstream& data)

// Pre: length has been set to zero.

// Post: Values have been read from file data and stored into

// list; length contains the number of values read.

{

int counter = 0;



while (data)

{

data >> items[counter].stockNumber >> items[counter].price

>> items[counter].quantity;

counter++;

}

length = counter - 1;

}



//*******************************************************



void ListType::PrintList()

// Pre: list has valid data.

// Post: Items in the list have been written on cout one

// per line.

{

int index;



for (index = 0; index < length; index++)

{

cout << items[index].stockNumber << " " << setprecision(2)<br>
<< items[index].price << " " << items[index].quantity << endl;<br>
}

cout << endl;<br>
cout << "The length is: " << length;<br>
}






The program reads from this file below called parts.dat



01 1.23 650

02 78.66 75

100 365.21 6

123 32.46 2

143 0.12 1000

32 11.11 66

43 12.21 55

44 1.45 23

65 14.44 7

77 16.90 10

87 334.55 3

88 10.95 95

98 4.88 22







Comments



  • >Please help me find the errors in this class program, it is suppose to


    I'll do what I can. I'll put comments in the code with //MJ:



    Error 24: Constructer cannot have a return type specification

    Error 37: Call to undefined function 'Getlist'

    Error 38: Call to PrintList in function main()

    Error 52: Undefined symbol 'list' in ListType::ListType()

    Warning 37: Structure passed by value in function main()














    // from file inventory, stores them in a list, and prints the list.



    #include

    #include

    #include



    const int MAX_LENGTH = 50;



    struct ItemType // declare record of items

    {

    int stockNumber;

    float price;

    int quantity;

    };


    class ListType // declare list class

    {

    private:

    int length;

    ItemType items[MAX_LENGTH];

    public:


    //MJ: Since you have the next method with the same name as the

    // class, it is therefore a constructor. Constructors don't

    // have return types. Your compiler error is telling you

    // that 'void' as a return type is not valid. Take it out.

    // Constructors and destructors are the only methods that are

    // not allowed to have a return type.


    void ListType(); //empty list

    void GetList(ifstream& myFile); //reads values

    void PrintList(); //prints list

    };



    int main ()

    {



    ListType list; // define list variable

    cout.setf(ios::fixed, ios::floatfield);

    cout.setf(ios::showpoint);

    ifstream inventory;

    inventory.open("parts.dat");


    //MJ: I presume what you really wanted to do here is to call the

    // GetList method within the ListType class for a particular

    // instantiation of ListType. Well, you've got an instantiation.

    // You called it list. To get the GetList method associated

    // with the object 'list', you must use the method calling

    // syntax: list.GetList(inventory); Note that calling the

    // method bears some resemblance to accessing a public data

    // member.

    GetList(inventory);


    //MJ: See previous comment.

    PrintList();

    return 0;

    }



    //*******************************************


    ListType::ListType()

    // Post: length has been set to zero.

    {

    int row;



    for (row = 0; row < MAX_LENGTH; row++)

    {

    //MJ: There is no 'list' variable accessible here. No, don't

    // make it a global. That's not the point. The point is

    // that when execution is in here, it must already be

    // associated with a particular object. In the constructor,

    // the object for which this method is being called is the

    // one being constructed.

    //

    // What this essentially means is that you already have access

    // to members of your class here. You don't (and can't) qualify

    // them with the 'list' variable. Consider that when execution

    // is in here, it is already 'inside the object' for which it

    // is constructing. This is true no matter how many

    // instantiations you create.

    //

    // So remove the 'list.'. It doesn't belong here. You _can_

    // qualify it by prepending 'this->' since 'this' is a compiler

    // provided pointer to the object the method is called on.

    // However even 'this->' isn't necessary here.

    //

    // It is because of the problem mentioned here that your compiler

    // is telling you that 'list' doesn't exist. It doesn't exist

    // here. All the constructor knows is that it is constructing

    // some instantiation.

    list.items[MAX_LENGTH].stockNumber = 0;

    list.items[MAX_LENGTH].price = 0;

    list.items[MAX_LENGTH].quantity = 0;

    }

    list.length = 0;

    }



    //*******************************************



    void ListType::GetList(ifstream& data)

    // Pre: length has been set to zero.

    // Post: Values have been read from file data and stored into

    // list; length contains the number of values read.

    {

    int counter = 0;



    while (data)

    {

    //MJ: Given your incorrect prepending of 'list.' in the

    // constructor, I'm confused that you don't do it here.

    // Never mind. This is correct.

    data >> items[counter].stockNumber >> items[counter].price

    >> items[counter].quantity;

    counter++;

    }

    length = counter - 1;

    }



    //*******************************************************



    void ListType::PrintList()

    // Pre: list has valid data.

    // Post: Items in the list have been written on cout one

    // per line.

    {

    int index;



    for (index = 0; index < length; index++)

    {

    cout << items[index].stockNumber << " " << setprecision(2)<br>
    << items[index].price << " " << items[index].quantity << endl;<br>
    }

    cout << endl;<br>
    cout << "The length is: " << length;<br>
    }






    The program reads from this file below called parts.dat



    01 1.23 650

    02 78.66 75

    100 365.21 6

    123 32.46 2

    143 0.12 1000

    32 11.11 66

    43 12.21 55

    44 1.45 23

    65 14.44 7

    77 16.90 10

    87 334.55 3

    88 10.95 95

    98 4.88 22







Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories