-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;
}
7
  • 3
    "isn't working" is not helpful at all. Commented Feb 24, 2018 at 12:20
  • 3
    Your best bet at this point are these C++ books. C++ can't be learned by guessing. Commented Feb 24, 2018 at 12:21
  • 1
    Change the constructor declaration to Group(Students *array , int size) or Group(Students array[] , int size). Commented Feb 24, 2018 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 Commented Feb 24, 2018 at 12:22
  • 1
    Your function expects array of Students pointers Commented Feb 24, 2018 at 12:30

2 Answers 2

0

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why not iterators for Group argument?
@JorgeBellón to keep it simple
-1
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; 
}

15 Comments

The code will work. But I think your explanations aren't exactly correct.
@Nicky C Which part?
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.
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.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.