Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;类定义中的结构数组问题_C++_Arrays_Class_Struct_Enums - Fatal编程技术网

C++ C++;类定义中的结构数组问题

C++ C++;类定义中的结构数组问题,c++,arrays,class,struct,enums,C++,Arrays,Class,Struct,Enums,我目前在尝试在类头文件中创建结构时遇到很多问题。在public accessors&mutators中,它说变量是“未声明的标识符”,而且我不确定如何从main.cpp文件引用结构数组,并将打开文件中的变量分配给结构数组中的元素 //头文件 #ifndef FOO_H #define FOO_H #include <string> #include <iostream> using namespace std; class Foo { private: stru

我目前在尝试在类头文件中创建结构时遇到很多问题。在public accessors&mutators中,它说变量是“未声明的标识符”,而且我不确定如何从main.cpp文件引用结构数组,并将打开文件中的变量分配给结构数组中的元素

//头文件

#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
\ifndef FOO\H
#定义FOO_H
#包括
#包括
使用名称空间std;
福班
{
私人:
结构袋
{
字符串名;
整数金额;
枚举税{食品、药品}类;
};
公众:
//不确定枚举类型为的构造函数
字符串getName()常量
{返回名称;}
int getAmount()常量
{返回金额;}
无效集合项(字符串);
无效设定金额(整数);
void setTaxCat(int);
};
静态常量浮动食品税=0.05,药品税=0.04;
#恩迪夫
//实施文件

#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); }
#包括“Foo.h”
#包括
使用名称空间std;
void Foo::setItem(字符串项)
{name=item;}
void Foo::setAmount(整数数量)
{金额=数量;}
void Foo::setTaxCat(int tx)
{type=static_cast(tx);}
//主程序

#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;
}
#包括“Foo.h”
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串项;
整数数量;
int-tx;
ifstream输入文件;
字符串文件名;
常量大小=20;
cout setItem(item);//不确定如何将项目发送到
getline(输入文件,数量,#);
项目[索引]->设置金额(数量);
getline(inputFile,tx,#’);
项目[索引]->setTaxCat(tx);
}
}
inputFile.close();
返回0;
}
下面是它引用的文件的示例

鸡蛋是食物


止咳药水#2#药物

您只声明了bag的定义,但从未将其创建为成员。
使包成为像这样的成员

    struct bag
    {
        string name;
        int amount;
        enum tax { food, medicine } type;
    } b;
//    ^^
您的方法应该如下所示

string getName() const
    { return b.name; }
int getAmount() const
    { return b.amount; }
...
尽管我会推荐
b
作为一名公众成员,以摆脱那些丑陋的获得者

由于
bag
类型是私有的,因此除非您这样做,否则无法创建它的数组

class Foo {
  friend int main();
  private:
    struct bag {
    ...
    }b;
  ...
};
现在,您可以在main中创建一个
bag
数组

Foo::bag items[1000];

使用名称空间std放置
向量项可以。我如何在主代码中引用结构数组以及将数据写入结构变量您不能,
bag
是私有数据类型,请将其公开。这应该可以,但为什么要这样做呢。如果Foo中只有一个实例off-bag(类型为private),则可以将bag的成员直接作为Foo的成员。我已将结构设置为公共结构,以便可以将其作为数组引用,但我仍然不确定如何从主代码引用它。