copy Vector to array

I need help finding what's wrong with this code(the function part). I understand the concept of copying the vector elements to an array but there an error on my main code when I call this function that says 'incompatible type void. Sorry if it's something simple. Any tips will help. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 void copy_to_array( vector<Student> &group ){
    int size = group.size(); 
    Student * ptr = new Student[size];
    for(int i = 0; i < size; i++){
        ptr[i] = group[i];
        ptr++;
    }//for
    return;
}//copy_to_array

//part of main
int main(){
    vector<Student> group;
    initialize(group); // reads in students from input, stores Student objects in the vector "group"
    
    int size = group.size();
    
    //declare a pointer to Student
    Student *ptr_to_array = NULL;
    //create an array of Students using "new" in the heap and copy content from the vector group into the array,
    //ptr_to_array must point to the beginning of this array
    ptr_to_array = copy_to_array( group );
Last edited on
well i found a small bug that I should not be incrementing the pointer (ptr) in the loop.
1
2
3
4
5
6
7
8
void copy_to_array( vector<Student> &group ){
    int size = group.size(); 
    Student * ptr = new Student[size];
    for(int i = 0; i < size; i++){
        ptr[i] = group[i];
    }//for
    return;
}//copy_to_array 


still having trouble why I cant have this line
 
ptr_to_array = copy_to_array( group );
copy_to_array is a void function, meaning it doesn't return anything.

1
2
3
// copy_to_array doesn't return anything so what
// value is there to assign to ptr_to_array?
ptr_to_array = copy_to_array( group );
Last edited on
Thank you for the reply I meant to fix that too. That's what I was confused about too my professor had us look for the bugs in a file without changing the main. I got most of the bugs I believe it's just that function and that line I feel like I may be missing something.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<vector>
#include<string>
using namespace std;

#include "student.h"

//this function does NOT have bugs
void initialize(vector<Student> &group);
/*
    original function given
    it contains two bugs according to my professor
void copy_to_array( vector<Student> &group ){
	int size = group.size();
	Student * ptr = new Student[size];

	for(int i = 0; i < size; i++){
		ptr[i] = group[i];
		ptr++;
	}//for
	return ptr;
}//copy_to_array
*/
//Contains bug(s)
void copy_to_array( vector<Student> &group ){
    int size = group.size();
    Student  *ptr = new Student[size];
    for(int i = 0; i < size; i++){
        ptr[i] = group[i];

    }//for
}//copy_to_array

//Contains bug(s)
void print_array(Student *ptr_to_array, int size){
    
    for(int i = 0; i < size; i++){
        ptr_to_array[i].print();//member function "print" of class Student is called
    }//for
    
}//print_array

//Contains bug(s)
void init_array_of_pointers(Student *array_of_ptrs[], int &array_size, vector<Student> &group){
    int group_size = group.size();
    int min_size = 0;
    for(int i = 0; i < array_size && i < group_size; i++){
        array_of_ptrs[i] = new Student ( group[i].get_name(), group[i].get_gpa() );
        min_size++;
    }//for
    array_size = min_size;
    
}//init_array_of_pointers

//Contains bug(s)
void print_via_pointers(Student *array_of_ptrs[], int size){
    
    for(int i = 0; i < size; i++){
        (*array_of_ptrs)->print();
        array_of_ptrs++;
    }
    
    cout << "The first student is:" << endl;
    array_of_ptrs[0]->print();
    
}//print_via_pointers

//Contains bug(s)
void release_space(Student *array_of_ptrs[], int size){
    for(int i = 0; i < size; i++){
        delete [] array_of_ptrs[i];
    }//for
}//release_space

//DO NOT CHANGE code inside the main() function
//function main() DOES NOT have bugs
int main(){
    vector<Student> group;
    initialize(group); // reads in students from input, stores Student objects in the vector "group"
    
    int size = group.size();
    
    //declare a pointer to Student
    Student *ptr_to_array = NULL;
    //create an array of Students using "new" in the heap and copy content from the vector group into the array,
    //ptr_to_array must point to the beginning of this array
    ptr_to_array = copy_to_array( group );
    
    //print students' information
    print_array(ptr_to_array, size);
    
    const int SIZE = 5;
    Student *array_of_ptrs[SIZE];//SIZE has to be constant int
    int updated_size = SIZE;//number of actually copied students from "group" into array of pointers
    //initialize array of pointers with pointers to Student objects with the same names and gpas as those in the vector "group"
    init_array_of_pointers(array_of_ptrs, updated_size, group);
    cout << "Total copied students into array of pointers is " << updated_size << endl;
    
    //print students information
    print_via_pointers(array_of_ptrs, updated_size);
    
    release_space(array_of_ptrs, updated_size);
    
    delete [] ptr_to_array;
    return 0;
}

//this function does NOT have bugs
void initialize(vector<Student> &group){
    string aname;
    double agpa;
    while( cin >> aname ){
        cin >> agpa;
        Student current(aname, agpa);//an object of type Student is created, and a constructor of the class initializes name with aname and gpa with agpa
        group.push_back(current);
    }
}//initialize does not have bugs 


this is student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

#include "student.h"

void Student::print(){
    cout << name << ", " << showpoint << fixed << setprecision(2) << gpa << endl;
}//print

void Student::set_gpa(double g){
    gpa = g;
}


and student.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<string>
using namespace std;

#ifndef STUDENT_H
#define STUDENT_H

class Student{
    
private:
    string name;
    double gpa;
public:
    Student(): name(""), gpa(0.0) {};
    Student(string n, double g): name(n), gpa(g) {};
    string get_name(){ return name; };
    double get_gpa(){ return gpa; };
    void print();
    void set_gpa(double g);
};

#endif 
Last edited on
okay I think since it has to return something since it is being assigned it can't be void.

1
2
3
4
5
6
7
8
9
Student copy_to_array( vector<Student> &group ){
	int size = group.size();
	Student * ptr = new Student[size];

	for(int i = 0; i < size; i++){
		ptr[i] = group[i];
		ptr++;
	}//for
	return ptr;


but still won't compile.
1
2
3
4
5
6
7
8
9
10
11
12
13
// Student copy_to_array( vector<Student> &group ){
Student* copy_to_array( const vector<Student> &group ){

	int size = group.size();
	Student* ptr = new Student[size];

	for(int i = 0; i < size; i++){
		ptr[i] = group[i];
		ptr++;
	}//for

	return ptr;
}
Last edited on
Thank you JLBorges I was able to figure it out, I was being careless on that.
Topic archived. No new replies allowed.