无法将数组值传递给构造函数中的向量

时间:2015-11-02 06:19:46

标签: c++ arrays vector constructor

我有点路障,我似乎无法弄清楚我哪里出错了。本质上,我只想通过构造函数将测试代码中的数组值传递给向量,然后打印向量的内容。无论出于何种原因,我甚至无法点击开始将数组值添加到矢量的for循环。

标题代码:

#pragma once

#ifndef BoxOfProduce_H
#define BoxOfProduce_H
#include <vector>
#include <iostream>
#include <cstdlib> //for exit
using namespace std;
class BoxOfProduce
{
public:
    BoxOfProduce();

    BoxOfProduce(string customerId, int size, bool doRandom, bool okDuplicates, std::string productList[], int listSize);

    void addBundle(string productList);
    void displayBox();
private:
    string customerID;

    vector<string> test;
    vector<string> bundles;
    bool allowDuplicates;
    bool buildRandom;
    int size;


};
#endif
// End of dayOfYear.h

BocOfProduce.cpp

#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
#include <cstdlib> //for exit
using namespace std;
#include "BoxOfProduce.h" 

vector<string> tempbundles;
bool isInVect = false;


    BoxOfProduce::BoxOfProduce()
    {
        customerID = "No Name";
        allowDuplicates = true;
        buildRandom = false;
    }

    BoxOfProduce::BoxOfProduce(string customerId, int size, bool doRandom, bool okDuplicates, string productList[], int listSize)
    {
        customerID = customerId;

        buildRandom = doRandom;
        allowDuplicates = okDuplicates;
        size = size;
        for (int k = 0; k > listSize; k++)
        {

            tempbundles.push_back(productList[k]);
            cout << "added to temp" << endl;
        }


        if (allowDuplicates == false)
        {
            for (int k = 0; k > listSize; k++)
            {

                tempbundles.push_back(productList[k]);
                cout << "added to temp" << endl;
            }

            for (int i = 0; i < listSize; i++)
            {
                for (int j = 0 + 1; j < listSize; j++)
                {
                    if (tempbundles[i] == bundles[j])
                    {
                        isInVect = true;
                    }

                    if (isInVect == false)
                    {
                        bundles.push_back(tempbundles[i]);
                        cout << "added to isinvect bundle" << endl;
                    }
                }

            }
        }
        else if (allowDuplicates == true)
        {

            for (int k = 1; k > listSize; k++)
            {
                bundles.push_back(productList[k]);
                cout << "added to normal bundle" << endl;
            }
        }

    }

    void BoxOfProduce::addBundle(string productList)
    {
        for (int k = 1; k > 100; k++)
        {
            bundles.push_back(productList);
        }
    }





    void BoxOfProduce::displayBox()
    {
        cout << "custome ID is: " << customerID << "\n" << endl;
        std::cout << std::boolalpha;
        cout << "-buildRandom set to " << buildRandom <<  endl;
        cout << "-allowDuplicates set to " << allowDuplicates <<  endl;
        cout << "Contents of box: " << customerID << " with size: " << size << endl;
        test.push_back("test");
        for (int i = 0; i < bundles.size(); i++)
        {
            cout << bundles[i] << "\n";
        }
        cout << "\n" << endl;
    }

测试代码:

#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <algorithm>
#include <cstdlib> //for exit
#include "BoxOfProduce.h" 
using namespace std;
int main()
{
    srand(1234);  // Seed random generator for random additions of products
    const int LISTSIZE = 12;
    string produceList[] = { "Broccoli", "Tomato", "Kiwi", "Kale", "Tomatillo",
        "Mango", "Spinach", "Cucumber", "Radish", "Chard", "Spinach", "Mango" };
    cout << "Original list of produce:" << endl;
    for (int i = 0; i < LISTSIZE; i++)
    {
        cout << "item[" << i << "] = " << produceList[i] << endl;
    }
    // Test BoxOfProduce class
    cout << endl << "Start with empty box0" << endl;
    BoxOfProduce box0;  // Default constructor creates empty box
    cout << "Display box0:" << endl;
    box0.displayBox();  // Display the empty box
    cout << endl;  // Skip a line
    cout << "Add all products from the produceList[] to box0 allowing duplicates:"
        << endl;
    for (int i = 0; i < LISTSIZE; i++)
        box0.addBundle(produceList[i]);  // Duplicates allowed in box
    cout << "Display box0 again after loading with products:" << endl;
    box0.displayBox();
    cout << endl;

    BoxOfProduce box1("Box-1", 4, false, true, produceList, LISTSIZE);
    box1.displayBox();
    BoxOfProduce box2("Box-2", 4, true, false, produceList, LISTSIZE);
    box2.displayBox();
    BoxOfProduce box3("Box-3", 8, true, true, produceList, LISTSIZE);
    box3.displayBox();
    BoxOfProduce box4("Box-4", 12, true, true, produceList, LISTSIZE);
    box4.displayBox();
    BoxOfProduce box5("Box-5", 12, true, false, produceList, LISTSIZE);
    box5.displayBox();  // This box produces an error message



}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你的问题是,如果重复你使用bundle和tempbundle的列表大小,这不应该修复检查BocOfProduce.cpp的代码

vector<string> tempbundles;
bool isInVect = false;


BoxOfProduce::BoxOfProduce()
{
    customerID = "No Name";
    allowDuplicates = true;
    buildRandom = false;
}

BoxOfProduce::BoxOfProduce(string customerId, int size, bool doRandom, bool okDuplicates, string productList[], int listSize)
{
    customerID = customerId;

    buildRandom = doRandom;
    allowDuplicates = okDuplicates;
    size = size;
    tempbundles = vector<int>() ; // set temp bundles to empty vector
    for (int k = 0; k < listSize; k++) //k was >listSize it should be <
    {

        tempbundles.push_back(productList[k]);
        cout << "added to temp" << endl;
    }

    if (allowDuplicates == false)
    {
        for (int k = 0; k < listSize; k++)
        {

            tempbundles.push_back(productList[k]);
            cout << "added to temp" << endl;
        }

        for (int i = 0; i < listSize; i++)
        {
            isInVect = false;
            for (int j = 0 + 1; j < bundles.size(); j++)
            {
                if (tempbundles[i] == bundles[j])
                {
                    isInVect = true;
                }

            }
            if (isInVect == false)
            {
                bundles.push_back(tempbundles[i]);
                cout << "added to isinvect bundle" << endl;
            }

        }
    }
    else if (allowDuplicates == true)
    {

        for (int k = 1; k < listSize; k++)
        {
            bundles.push_back(productList[k]);
            cout << "added to normal bundle" << endl;
        }
    }

}

void BoxOfProduce::addBundle(string productList)
{
    for (int k = 1; k > 100; k++)
    {
        bundles.push_back(productList);
    }
}





void BoxOfProduce::displayBox()
{
    cout << "custome ID is: " << customerID << "\n" << endl;
    std::cout << std::boolalpha;
    cout << "-buildRandom set to " << buildRandom <<  endl;
    cout << "-allowDuplicates set to " << allowDuplicates <<  endl;
    cout << "Contents of box: " << customerID << " with size: " << bundles.size() << endl;
    test.push_back("test");
    for (int i = 0; i < bundles.size(); i++)
    {
        cout << bundles[i] << "\n";
    }
    cout << "\n" << endl;
}