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

4
  • 4
    Placing using namespace std; in a header file is not a good programming practice. Commented May 1, 2013 at 20:02
  • 1
    In order to solve your homework by yourself start by reading a good C++ book: stackoverflow.com/questions/388242/… . Commented May 1, 2013 at 20:03
  • Instead of bag[items] array store the items in a vector member of Foo class. Commented May 1, 2013 at 20:13
  • Where is the point of having the class Foo at all (in this code). Have struct bag and vector<bag> items; will do. Commented May 1, 2013 at 20:19

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

4 Comments

How do I go about referencing the struct array in the main code as well as writing data to the struct variables
you cannot, bag is private data type, make it public.
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.