【问题标题】:Can't pass array values to a vector from within a constructor无法从构造函数中将数组值传递给向量
【发布时间】:2015-11-02 06:19:46
【问题描述】:

我遇到了一些障碍,我似乎无法弄清楚我哪里出错了。本质上,我只想通过构造函数将测试代码中数组的值传递给向量,然后打印向量的内容。无论出于何种原因,我什至无法点击开始将数组值添加到向量的 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



}

感谢您的所有帮助!

【问题讨论】:

  • 您收到什么错误?
  • 我实际上没有收到任何错误。当它运行时,除了矢量内容外,其他所有内容都会打印出来。
  • 既然可以将原始字符串数组声明为向量,为什么还要麻烦呢?
  • for (int k = 0; k &gt; listSize; k++) 如果 listSize != 0,这个 for 循环将永远不会执行
  • @Cyber​​Spock 不幸的是,这些是我必须遵循的准则。谢谢你指出这个错误!!这么傻。

标签: c++ arrays vector constructor


【解决方案1】:

您的问题是,在重复的情况下,您使用 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;
}

【讨论】:

  • 不,我仍然得到一个空的打印。我什至根本没有在循环中打印“添加到临时”。我猜它永远不会到达那里,但我似乎无法理解为什么。
  • 检查我尝试过的编辑代码,它现在打印列表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多