正向声明并动态分配所声明类的指针数组? 我一直在用C++实验室解决这个问题。据我所知,我的工作都在进行,除了我的教授在我们的作业中规定的这一条:< /P>。
源文件中类声明的顺序是InventorySystem、InventoryItem、Product和eProduct。由于InventorySystem包含指针的InventoryItem数组,所以必须对InventoryItem使用前向声明 因此,InventoryItem->Product->eProduct在派生的heriarchy中相互关联,我们的任务是将继承权与InventorySystem类结合起来编写,以管理指向eProduct对象的指针数组 不幸的是,我所有的StackOverflow帖子都让我得出这样的结论:对我的要求是不可能的。正如我所理解的,转发声明“只有通知编译器类存在才是真正有用的”,任何与代码的结构或定义相关的上下文都不适用于转发声明——这似乎与我实验室的其他要求直接冲突,因为InventorySystem需要一个名为BuildInventory的方法,该方法解析格式化的文本文件,并动态地将指针数组分配给eProduct对象。这不需要“前向声明”对象的构造函数吗 我真的真的希望我是一个C++新手,而且我被误导了,因为这个问题让我整天都疯了。p> 提前感谢你的帮助 PS:很抱歉函数名和变量的奇怪大小写,这是我的教授在我们的作业中写大小写的方式,我认为只使用他建立的内容更安全正向声明并动态分配所声明类的指针数组? 我一直在用C++实验室解决这个问题。据我所知,我的工作都在进行,除了我的教授在我们的作业中规定的这一条:< /P>。,c++,forward-declaration,C++,Forward Declaration,源文件中类声明的顺序是InventorySystem、InventoryItem、Product和eProduct。由于InventorySystem包含指针的InventoryItem数组,所以必须对InventoryItem使用前向声明 因此,InventoryItem->Product->eProduct在派生的heriarchy中相互关联,我们的任务是将继承权与InventorySystem类结合起来编写,以管理指向eProduct对象的指针数组 不幸的是,我所有的StackOverfl
//Format for text file is Name;Quantity;Price;Condition
void BuildInventory()
{
ifstream fin ("in.txt");
string name="";
string Buffer = "";
int quantity = 0;
double price = 0.0;
int temp = 0;
if (!fin) {
cout << "ERROR: Failed to open input file\n";
exit(-1);
}
while ( getline (fin, Buffer, ';') ) {
string condChar = "";
Condition condition = NEW;
name = Buffer;
getline (fin, Buffer, ';');
quantity = atol (Buffer.c_str ( ) );
getline (fin, Buffer, ';');
price = atof (Buffer.c_str( ) );
getline (fin, Buffer, '\n') ;
condChar = Buffer.c_str();
if(condChar.compare("R") == 0)
condition = REFURBISHED;
else if(condChar.compare("U") == 0)
condition = USED;
else if(condChar.compare("D") == 0)
condition = DEFECTIVE;
ep = new eProduct(name, quantity, price , condition);
ItemList[ItemCount] =ep;
++ItemCount;
fin.ignore(1, '\n');
}
fin.close();
Sort();
}
秘密在于你的教授给你的说明,以及你自己的描述: 源文件中类声明的顺序是InventorySystem、InventoryItem、Product和eProduct。由于InventorySystem包含InventoryItem指针数组,因此必须对InventoryItem使用前向声明 我们的任务是编写与InventorySystem类相结合的继承机制,以管理指向eProduct对象的指针数组 所以你需要这样的东西:
class InventoryItem; // forward declaration
class InventorySystem
{
...
InventoryItem* ItemList[TheArraySizeHere]; // array of InventoryItem pointers
...
};
class InventoryItem
{
...
};
class Product : public InventoryItem
{
...
};
class eProduct : public Product
{
...
};
由于eProduct
源自InventoryItem
,因此可以将eProduct
指针存储在InventoryItem
指针数组中
这是谜题的另一部分。由于尚未声明eProduct
类,因此无法在InventorySystem
类声明中内联实现BuildInventory()。BuildInventory()
的实现需要在定义了eProduct
之后进行分离和实现,例如:
class InventoryItem; // forward declaration
class InventorySystem
{
...
InventoryItem* ItemList[TheArraySizeHere]; // array of InventoryItem pointers
...
void BuildInventory();
};
class InventoryItem
{
...
};
class Product : public InventoryItem
{
...
};
class eProduct : public Product
{
...
};
...
void InventorySystem::BuildInventory()
{
// implementation here ...
}
这通常是通过将所有声明放在.h
文件中,并将所有实现放在.c
/.cpp
文件中来完成的,该文件包含.h
文件,例如:
清单h:
#ifndef InventoryH
#define InventoryH
class InventoryItem; // forward declaration
class InventorySystem
{
...
InventoryItem* ItemList[TheArraySizeHere]; // array of InventoryItem pointers
...
};
class InventoryItem
{
...
InventoryItem(std::string Name, int Quantity);
...
};
class Product : public InventoryItem
{
...
Product();
Product(string Name, int Quantity, double Price);
...
};
class eProduct : public Product
{
...
eProduct(string Name, int Quantity, double Price, Condition condition);
...
};
#endif
Inventory.cpp:
#include "Inventory.h"
//Constructor of InventoryItem
InventoryItem::InventoryItem(std::string Name, int Quantity)
{
this->Name = Name;
this->Quantity = Quantity;
}
//Constructor of Product
Product::Product() : ProductID(0), Price(0.0) {}
Product::Product(string Name, int Quantity, double Price)
: InventoryItem(Name, Quantity)
{
this->Price = Price;
this->ProductID = generateProductID();
}
//Constructor of eProduct
eProduct::eProduct(string Name, int Quantity, double Price, Condition condition)
: Product(Name, Quantity, Price)
{
this->condition = condition;
}
void InventorySystem::BuildInventory()
{
// implementation here ...
}
秘密在于你的教授给你的说明,以及你自己的描述:
源文件中类声明的顺序是InventorySystem、InventoryItem、Product和eProduct。由于InventorySystem包含InventoryItem指针数组,因此必须对InventoryItem使用前向声明
我们的任务是编写与InventorySystem类相结合的继承机制,以管理指向eProduct对象的指针数组
所以你需要这样的东西:
class InventoryItem; // forward declaration
class InventorySystem
{
...
InventoryItem* ItemList[TheArraySizeHere]; // array of InventoryItem pointers
...
};
class InventoryItem
{
...
};
class Product : public InventoryItem
{
...
};
class eProduct : public Product
{
...
};
由于eProduct
源自InventoryItem
,因此可以将eProduct
指针存储在InventoryItem
指针数组中
这是谜题的另一部分。由于尚未声明eProduct
类,因此无法在InventorySystem
类声明中内联实现BuildInventory()。BuildInventory()
的实现需要在定义了eProduct
之后进行分离和实现,例如:
class InventoryItem; // forward declaration
class InventorySystem
{
...
InventoryItem* ItemList[TheArraySizeHere]; // array of InventoryItem pointers
...
void BuildInventory();
};
class InventoryItem
{
...
};
class Product : public InventoryItem
{
...
};
class eProduct : public Product
{
...
};
...
void InventorySystem::BuildInventory()
{
// implementation here ...
}
这通常是通过将所有声明放在.h
文件中,并将所有实现放在.c
/.cpp
文件中来完成的,该文件包含.h
文件,例如:
清单h:
#ifndef InventoryH
#define InventoryH
class InventoryItem; // forward declaration
class InventorySystem
{
...
InventoryItem* ItemList[TheArraySizeHere]; // array of InventoryItem pointers
...
};
class InventoryItem
{
...
InventoryItem(std::string Name, int Quantity);
...
};
class Product : public InventoryItem
{
...
Product();
Product(string Name, int Quantity, double Price);
...
};
class eProduct : public Product
{
...
eProduct(string Name, int Quantity, double Price, Condition condition);
...
};
#endif
Inventory.cpp:
#include "Inventory.h"
//Constructor of InventoryItem
InventoryItem::InventoryItem(std::string Name, int Quantity)
{
this->Name = Name;
this->Quantity = Quantity;
}
//Constructor of Product
Product::Product() : ProductID(0), Price(0.0) {}
Product::Product(string Name, int Quantity, double Price)
: InventoryItem(Name, Quantity)
{
this->Price = Price;
this->ProductID = generateProductID();
}
//Constructor of eProduct
eProduct::eProduct(string Name, int Quantity, double Price, Condition condition)
: Product(Name, Quantity, Price)
{
this->condition = condition;
}
void InventorySystem::BuildInventory()
{
// implementation here ...
}
问题
InventorySystem有一个名为BuildInventory的方法,该方法解析格式化的文本文件并动态分配指向eProduct对象的指针数组。这不需要“前向声明”对象的构造函数吗
回答
是的,这将需要eProduct
的完整类定义以及从InventoryItem
派生的任何其他叶级类。但是,这是在类的实现中实现的
类的定义仍然可以继续使用指向转发的声明类的指针。问题
InventorySystem有一个名为BuildInventory的方法,该方法解析格式化的文本文件并动态分配指向eProduct对象的指针数组。这不需要“前向声明”对象的构造函数吗
回答
是的,这将需要eProduct
的完整类定义以及从InventoryItem
派生的任何其他叶级类。但是,这是在类的实现中实现的
类的定义仍然可以继续使用指向转发声明类的指针。转发声明允许您指定转发声明类型的指针和引用,还可以在函数声明中使用类型(在