Classes and pointers to a vector

closed account (oEwqX9L8)
I have an assignment to use a previously created class and vector to take names as input, create and assign them to a new pointer, and then store them in the vector. I have been trying it for days and can't come up with the correct syntax.
This is the code I am given to start with and the exact instructions:

"With each name, create a Student pointer and insert it into the "students" vector. You must print all the names in the vector in reverse order."

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
#pragma once
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Student
{
private:
	string name;

public:
	Student(string name);
	string getName();
	void setName(string newName);


	~Student();
};

Student::Student(string studentName)
{
	name = studentName;
}

string Student::getName()
{
	return name;
}

void Student::setName(string newName)
{
	name = newName;
}

Student::~Student()
{
}

//
// You may insert a function here

int main()
{
	vector<Student*> students;
	

	return 0;
}


Here is what I have added so far. Am I even going in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	vector<Student*> students;

	string names;
	
	do {
		cin >> names;
		
		Student* s = new Student(names);

		students.push_back(*s);


	} while (names != "STOP");

	return 0;
}


Thanks for any hints or examples that could point me in the right direction!]
For the most part it's okay, just a few notes:
* The compiler will reject line 12. I'll let you figure out why.
* Don't forget to release the pointers in the vector.
* 'names' is not a good name for a variable that will hold exactly one name at any time.
closed account (oEwqX9L8)
I think I have fixed the things pointed out by helios but I'm still having issues when I try and print the vector. I think it just returns the same memory address for each spot in the vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	vector<Student*> students;

	string student_name;

	do {
		cin >> student_name;

		Student* s = new Student(student_name);
		
		students.push_back(s);		
		
		delete s;

	} while (student_name != "STOP");

	for (int i = students.size() - 1; i >= 0; i--) {
		cout << students[i] << endl; 
	}
	
	system("pause");
	return 0;
}


Output:
1
2
3
4
5
6
005799E0
0057A458
005799E0
005799E0
005799E0
005799E0


Thanks again for any hints in the right direction, it's much appreciated!
You can print std::strings using std::cout <<, but note that your vector stores pointers to std::strings, not the std::strings themselves. You need to dereference the pointers in order to print the strings.

Also, now you're releasing the pointers too early. If you to do what I said in the previous paragraph, the program will crash.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<int *> v;
int input;

// Allocation phase
do{
    std::cin >> input;
    v.push_back(new int(input));
}while(input != 0);

// Usage phase
for (size_t i = 0; i < v.size(); i++)
    std::cout << *v[i] << std::endl;

// Deallocation phase
for (size_t i = 0; i < v.size(); i++)
    delete v[i];
Topic archived. No new replies allowed.