Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I create a class as Peers and i have some variable in this class. One of my variable is an array. And i create some typedef list of my class. Until here every thing is right but may array " int AvailableResource[30];" coud't take value and have error !!! i dont now how i can fix it.
help me please.

The error occurs exactly in this section=>
UploadRate(ur), DownloadRate(dr), AnswerRate(ar), CHNumber(chn), AvailableResource(AR){}

What I have tried:

<pre>class Peers{
    public:
        int UploadRate; 
        int DownloadRate; 
        int AnswerRate;
        int CHNumber;
        int AvailableResource[30];

    public:
        Peers(int ur=0, int dr=0, int ar=0, int chn=0 , int AR=0):
            UploadRate(ur), DownloadRate(dr), AnswerRate(ar), CHNumber(chn),  AvailableResource(AR){}

    };

    typedef std::list<Peers> BaseList;
    BaseList FreindlyList[5], ReqularList[5], NewCH[5];
Posted
Updated 21-Jan-23 3:14am

As you omitted to tell us what the error message is I am guessing that the problem is the way you try to assign a valure to the array. Try this:
C++
Peers(int ur=0, int dr=0, int ar=0, int chn=0 , int AR=0):
    UploadRate(ur), DownloadRate(dr), AnswerRate(ar), CHNumber(chn),  AvailableResource{AR}{}
                                                                                       ^  ^
                                                                                       |  |
                                                            Use braces here, not parentheses.

In future please remember to include the complete text of the error message.
 
Share this answer
 
You are trying to initialise the int array AvailableResource with a single int value AR. That's not allowed, hence the compiler, correctly, emits an error.
If you really wish to initialize the array with caller supplied values then you should pass an array (or another kind of container) to the Peers constructor. Something like
C++
Peers(int ur=0, int dr=0, int ar=0, int chn=0 , int AR[]=nullptr, size_t ARsize=0):
            UploadRate(ur), DownloadRate(dr), AnswerRate(ar), CHNumber(chn)
{
  if ( AR != nullptr)
  {
    for (size_t n = 0; n<ARsize && n<30; ++n)
      AvailableResource[n] = AR[n];
  }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900