类数组的构造函数 [英] Constructor of class of an array

查看:569
本文介绍了类数组的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误在这code

 类业务
{
    上市:
        商业(); //默认的构造函数
        业务(串业务内容,INT R)
        {
            业务内容=业务内容;
            收视率= R;
        } //构造函数;    私人的:        串业务内容;
        诠释评级;
        INT items_owned;
        INT business_cancellation;
        INT biz_size_of_building;
        INT biz_shipping_method;
};INT主(INT ARGC,CHAR *的argv [])
{
    业务B [10];    B〔b_count(业务内容,评价);    返回0;
}

它给了我下面的错误( http://ideone.com/Ff​​ajNS ):

  prog.cpp:在函数'主INT(INT,CHAR **):
prog.cpp:32:32:错误:不对应的呼叫'(业务)(标准::字符串&放大器;,&INT放大器;)


解决方案

您正在试图调用一个已经构造的对象......默认的构造函数数组创建过程中调用一个构造函数,因此,你不能构造的再次反对。对于您的应用程序,你可能要考虑使用的std ::矢量在这里您可以使用从一组默认参数初始化的对象创建阵列状的容器。

例如,你可以这样做:

 的std ::矢量<商业及GT;阵列(10,经营(business_string,等级));

i am getting error in this code

class business
{
    public:
        business();  // Default Constructor
        business(string busines,int r)
        {
            busines=busines;
            ratings=r;
        }   // constructor;

    private:

        string busines;
        int ratings;
        int items_owned;
        int business_cancellation;
        int biz_size_of_building;
        int biz_shipping_method;
};

int main(int argc, char *argv[])
{
    business b[10];

    b[b_count](busines,rating);

    return 0;
}

It gives me the following error (http://ideone.com/FfajNS):

prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:32:32: error: no match for call to ‘(business) (std::string&, int&)’

解决方案

You're attempting to call a constructor on an already constructed object ... the default constructor was called during the array creation, therefore you cannot "construct" the object again. For your application, you may want to look into using a std::vector where you can create a array-like container using an object initialized from a set of default arguments.

For instance, you could do:

std::vector<business> array(10, business(business_string, rating));

这篇关于类数组的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆