类中的结构的c ++向量

时间:2018-03-08 21:40:28

标签: c++ class object vector struct

你好,我正在研究一个名为库存调查员的学校项目。规格如下:

enter image description here

到目前为止,我已经创建了一个类,其中包含结构和此结构的向量。 所有我试图做到目前为止是让类显示结构只是为了知道它的工作,但当我编译它并运行它没有任何反应。这是代码。原谅我所犯的任何新手错误,我对类和矢量都很新。提前谢谢你!

//Inventory Inquisitor.cpp
#include <iostream>
#include <string>
#include <cctype> //for toupper
#include <fstream>
#include <vector>
using namespace std;

class Inventory
{
  private:
  struct item
   {
  string Description = " ";
  double Quantity = 0;
  double Wholesalescost = 0;
  double Retailcost = 0;
  string Dateadded = " ";
   };
   vector<item> Inv;
public:
  void Display();

};

void Inventory::Display()
{ 

 Inv[0].Description = "english";
 Inv[0].Quantity = 1;
 Inv[0].Wholesalescost = 100;
 Inv[0].Retailcost = 200;
 Inv[0].Dateadded = "3/8/2018";
 cout << Inv[0].Description << endl;
 cout << Inv[0].Quantity << endl;
 cout << Inv[0].Wholesalescost << endl;
 cout << Inv[0].Retailcost << endl;
 cout << Inv[0].Dateadded << endl;
}

int main()
{
 Inventory inst1;

  inst1.Display();

 }

1 个答案:

答案 0 :(得分:0)

在访问之前,您必须将某些内容放入向量中:

void Inventory::Display(int index)
{ 
    // Print an item already in the vector
    if (index >= 0 && index < Inv.size()) {
        cout << Inv.at(index).Description << endl;
        cout << Inv.at(index).Quantity << endl;
        cout << Inv.at(index).Wholesalescost << endl;
        cout << Inv.at(index).Retailcost << endl;
        cout << Inv.at(index).Dateadded << endl;
    }
}

根据您的作业,您很可能会更改为打印现有项目的功能。您将有另一个功能来向向量添加项目。

<header class="header">
<img src="{% static 'img/headerWAI.jpg' %}" alt="HeaderWai jpg" class="headerimg" />
<h1 class="headerText">Waste Annotation Image</h1>
<p class="usermenu">
  {% block loginuser %}
    {% if user.is_authenticated %}
      Hello {{ user.username }}!
      <p><a class="loginlink" href="{% url 'logout' %}">logout</a>
      {% if user.is_staff %}
        <a href="{% url 'newimage' %}">manage image</a></p>
      {% endif %}
      {% else %}
      <p>You are not logged in : <a href="{% url 'login' %}">login</a></p>
    {% endif %}
  {% endblock %}
</p>