如果条件为真,则c ++实例化几个类

时间:2016-05-17 18:10:25

标签: c++

我有办法让这项工作成功吗?

如果年龄大于17,我想创建5个银行实例,但我收到此错误:

  

[错误]没有匹配函数来调用'Bank :: Bank()'

我需要在学校完成这项任务。

DOMAIN_CURRENT_SITE

2 个答案:

答案 0 :(得分:1)

Bank customer[20];

这里你默认构造了20个Bank个对象(奇怪地称为customer ??)。或者,至少,您尝试过,但您的Bank类没有默认构造函数。我也不应该这样说。

Bank customer[y](name,state,age);
cout << customer[y].getname();

在这里,我猜你试图“声明”单个数组元素,但它不能像那样工作。

您对y的使用也是错误的;您可以接受y最大值,但您可能打算使用当前循环计数器值k。这也是因为你有一个年龄过滤器,所以你可能会跳过一些数组元素。

为什么不使用美味的std::vector,并随意添加新客户?然后你也可以摆脱那些令人困惑的int,其中一半是你甚至不使用的。

int main()
{
    int y = 0;

    cout << "===============================\n";
    cout << "Welcome To Hojma Bank.Plc\n";
    cout << "How many accounts do you want to create?" << endl;
    cin >> y;

    std::vector<Bank> customers;

    for (int i = 0; i < y; i++) {
        int age;
        string name;
        string state;

        cout << "Please input your full name" << endl;
        cin >> name;
        cout << "Please input your state of origin" << endl;
        cin >> state;
        cout << "Please input your age" << endl;
        cin >> age;

        if (age >= 18){
            customers.emplace_back(name,state,age);
            cout << customer.back().getname();
        }
    }
}

您也可以对用户输入进行一些错误检查。并将该类重命名为Customer

答案 1 :(得分:0)

如果创建对象数组(func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("imagesCellIdentifier", forIndexPath:indexPath) as! cellcontroler cell.secondImageView?.image = self.photosGalleryArray[indexPath.row] return cell } ),则该类需要具有默认构造函数。只需定义如下内容:

Bank customer[20]