0

I'm currently having a lot of issues in trying to create a struct in a class header file. In the public accessors & mutators its saying that the variables are 'undeclared identifiers', as well as i'm unsure of how to reference the struct array from the main .cpp file and assign variables from an opened file to the elements in the struct array.

//header file

#ifndef FOO_H
#define FOO_H
#include <string>
#include <iostream>
using namespace std;

class Foo
{
private:
    struct bag
    {
        string name;
        int amount;
        enum tax { food, medicine } type;
    };
public:
    //Unsure about constructor with enum type
    string getName() const
        { return name; }
    int getAmount() const
        { return amount; }

    void setItem(string);
    void setAmount(int);
    void setTaxCat(int);
};

static const float foodTax = .05, medicineTax = .04;
#endif

//Implementation file

#include "Foo.h"
#include <string>
using namespace std;

void Foo::setItem(string item)
{ name = item; }
void Foo::setAmount(int qty)
{ amount = qty; }
void Foo::setTaxCat(int tx)
{ type = static_cast<tax>(tx); }

//Main program

#include "Foo.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string item;
    int qty;
    int tx;
    ifstream inputFile;
    string fileName;
    const in size = 20;

    cout << "Enter file name: ";
    getline(cin, fileName);
    inputFile.open(fileName.c_str(), ios::in);

    bag items[]  //Unsure how to create array from the struct in the class header file

    for(int index = 0; index < size; index++)
    {
        while(index < size && !inputFile.eof())
        {
            getline(inputFile, item, '#');
            items[index]->setItem(item);  //unsure how to send items to 

            getline(inputFile, qty, '#');
            items[index]->setAmount(qty);

            getline(inputFile, tx, '#');
            items[index]->setTaxCat(tx);
        }
    }
    inputFile.close();
    return 0;
}

here's an example of the file it's referencing

Eggs#12#food

Cough Drops#2#medicine

Dahaka
  • 305
  • 1
  • 4
  • 6
  • 4
    Placing `using namespace std;` in a header file is not a good programming practice. – Mahesh May 01 '13 at 20:02
  • 1
    In order to solve your homework by yourself start by reading a good C++ book: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list . – SomeWittyUsername May 01 '13 at 20:03
  • Instead of bag[items] array store the items in a vector member of Foo class. – suspectus May 01 '13 at 20:13
  • Where is the point of having the class Foo at all (in this code). Have struct bag and `vector items;` will do. – hr_117 May 01 '13 at 20:19

1 Answers1

2

you only declare a definition for bag, but never create one as member.
make bag a member like

    struct bag
    {
        string name;
        int amount;
        enum tax { food, medicine } type;
    } b;
//    ^^

your method should look like

string getName() const
    { return b.name; }
int getAmount() const
    { return b.amount; }
...

Although I would recommend b as a public member as get rid of those ugly getters

Since bag type is private, you cannot make an array of it unless you do this

class Foo {
  friend int main();
  private:
    struct bag {
    ...
    }b;
  ...
};

now you can make a bag array in main like

Foo::bag items[1000];
yngccc
  • 5,594
  • 2
  • 23
  • 33
  • How do I go about referencing the struct array in the main code as well as writing data to the struct variables – Dahaka May 01 '13 at 20:08
  • 1
    you cannot, `bag` is private data type, make it public. – yngccc May 01 '13 at 20:10
  • 1
    This should work, but why should one do something like this. If there is only one instance off bag in Foo (and the type is private) you can put the members of bag direct as members of Foo. – hr_117 May 01 '13 at 20:11
  • I've made the struct public so I could reference it as an array, I'm still unsure of how to reference it from the main code. – Dahaka May 01 '13 at 20:21