C++ Issues with Struct array in class definition

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

Answers


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];

Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.