-2

I want to pass students to group , but my code isn't working I only started coding, so could anyone explain how it works

//file Group.h

class Group{

public:
    Group(Students *array[] , int size); // pointer. hard to understand

};

//main.cpp
int main() {

         int number = 9;
         Students students[number];
         Group group(students ,number) //build failed. for some reason

return 0;
}
bolov
  • 72,283
  • 15
  • 145
  • 224
  • 3
    "isn't working" is not helpful at all. – bolov Feb 24 '18 at 12:20
  • 3
    Your best bet at this point are these [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). C++ can't be learned by guessing. – Ron Feb 24 '18 at 12:21
  • 1
    Change the constructor declaration to `Group(Students *array , int size)` or `Group(Students array[] , int size)`. – 273K Feb 24 '18 at 12:21
  • `Students *array[]` in function parameter is actually a `Students * *`. In C++ arrays can not be passed into function by value. And to declare array in C++ `number` must be a compile-time constant. – user7860670 Feb 24 '18 at 12:21
  • 8
    *"build failed. for some reason"* - I'm pretty sure the compiler told you the exact reason it failed. Otherwise you should get a better compiler – UnholySheep Feb 24 '18 at 12:22
  • 1
    Your function expects array of Students pointers – Killzone Kid Feb 24 '18 at 12:30
  • 1
    The declaration of the array in your constructor parameter is different from the declaration of the array you pass to it. It contains a `*` (making it an array of *pointers*). Also it is not legal to have variable length local arrays in `C++` you should probably set the warning level on your compiler much higher. – Galik Feb 24 '18 at 12:38

2 Answers2

2

Don't use pointers in C++. And for the love of god, don't use raw new/delete. Use std::vector.

class Group
{
public:
    Group(const std::vector<Students>& v); // no pointers, easy to understand and use
};

int main()
{

         int size = 9;
         std::vector<Students> students(size);

         Group group{students}; 

return 0;
}
bolov
  • 72,283
  • 15
  • 145
  • 224
-2
class Group
{public:
    Group(Students *array[], int size); // This is actually an "array of
                                        // pointers" to Students, not
                                        // an array of Students
};

int main()
{
    int number = 9; 
    Students students[number]; // The size of an array in standard C++ must 
                               // be a constant value. This is a variable
                               // length array issue, which C++ doesn't support

    Group group(students, number); // Build failed for three reasons. 
                   // 1) Because array size wasn't a constant value. 
                   // 2) Because the first constructor argument is the wrong type.
                   // 3) You're trying to call a constructor which has not been defined

    return 0; 
}

To get this to work the way you want to there are three changes you need to make:

class Group
{public:
    Group(Students array[], int size){}   // Now it's an array of Students
                                          // Also note it has squiggly
                                          // brackets after it, that means it
                                          // has a definition
};

int main()
{
    const int number = 9; // Array size is now a constant
    Students students[number];

    Group group(students, number); // Now you can call the constructor because
                                   // it's been defined

    return 0; 
}
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • The code will work. But I think your explanations aren't exactly correct. –  Feb 24 '18 at 12:52
  • @Nicky C Which part? – Zebrafish Feb 24 '18 at 12:53
  • 1
    `Students *array[]` is not an array and redeclaring it as `Students array[]` won't make it array either. Also use of raw pointers in C++ is not encouraged. – user7860670 Feb 24 '18 at 12:54
  • @VTT The argument type is an array, it just decays to a pointer. – Zebrafish Feb 24 '18 at 12:55
  • @VTT It doesn't matter whether it's encouraged or not. He wanted to know what the problem with his code was, I explained it. If he wants to write an older style of C++ he's perfectly allowed. – Zebrafish Feb 24 '18 at 12:57
  • See [When a function has a specific-size array parameter, why is it replaced with a pointer](https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p). tl; dr; array function parameters are always adjusted to be raw pointers. – user7860670 Feb 24 '18 at 12:59
  • No, it does not "decay". Actually, it cannot "decay" because it is not an array object. It is a parameter. An array parameter is *adjusted* into a pointer parameter. –  Feb 24 '18 at 13:01
  • @Nicky C The array you pass in decays. The term "decays" isn't my invention. – Zebrafish Feb 24 '18 at 13:06
  • No. Read my comment again. Distinguish a parameter and an object passed as an argument. We are talking about parameter. –  Feb 24 '18 at 13:09
  • It only decays because `array` parameter of constructor is actually a pointer (note that array function parameters adjusted to be pointers is different from arrays passed into that function decaying to pointer). It wouldn't decay if `array` was declared as a reference to array for example. – user7860670 Feb 24 '18 at 13:09
  • @VTT I see, then there's absolutely no difference between Students *array[] and Students * * array. Well what should I have said instead, he wanted to pass an array, should I have said it's a pointer instead? – Zebrafish Feb 24 '18 at 13:11
  • 1
    Maybe... But I actually think that this question is a duplicate. The subject of passing "arrays" into function is rather confusing and questions like this appear on the regular basis. So you probably shouldn't spent time writing such a long answer at all. – user7860670 Feb 24 '18 at 13:19
  • _then there's absolutely no difference between Students *array[] and Students * *_ Similar to how you can have `char *argv[]` or `char **argv` in `main` – Killzone Kid Feb 24 '18 at 14:21
  • @VTT After looking around I really have to call BS on this nitpick. Among plenty of tutorials I've even seen Stroustrup mention these functions as taking arrays. Example from Stroustrup's book: "Drills: Define a function f() taking an int array argument and an int argument." Surely you'd tell him there's no such thing as a function that takes an array. Also his site: bool is_palindrome(const char s[ ] , int n) // s points to the first character of an array of n characters There's obviously sometimes cases for making such a distinction even though we all know they turn to ptrs – Zebrafish Feb 24 '18 at 15:11
  • 1
    @Zebrafish But I only wrote some comments. As for arrays as function parameters I think they should be completely banned or at least there should be some warning when implicit adjustment to pointer happens. – user7860670 Feb 24 '18 at 15:52