Still not getting pointers

Im doing an exercise where I put integers into a vector, remove them from the vector, and then display on user input. The first function with the template was an example provided by my book. the error is in the next two functions that I tried to create. I receive an error that I have illegal indirection

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
 #include <iostream>
#include <vector>
using namespace std;

template <typename T>
void DisplayVector(const vector<T>& inVec)
{
	for (auto element = inVec.cbegin(); element != inVec.cend(); ++element)
		cout << *element << ' ';

	cout << endl;
}


void AddVector(vector<int>& addVec)
{
	int userInput = 0;
	cout << "Please enter an integer tto add to the vector" << endl;
	cin >> userInput;
	*addVec.push_back(userInput);
	return;
}


void RemVector(vector<int>& remVec)
{
	
	*remVec.pop_back(); 
	return;
}

int main()
{
	vector<int> integers;
	int loop = 0;
	while (loop == 0)
	{
		cout << "would you like to (1)add, (2) remove, or (3)display the vector " << endl;
		int userInput = 0;
		cin >> userInput;
		if (userInput == 3)
			DisplayVector(integers);
		else if (userInput == 2)
			RemVector(integers);
		else if (userInput == 1)
			AddVector(integers);
		else
			cout << " Please select a validoption fropm 1-3 " << endl;

		cout << " Enter 1 to quit, any other entry to continue " << endl;
		userInput = 0;
		cin >> userInput;
		if (userInput == 1)
			loop = 1;
	}
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
void AddVector( std::vector<int>& vec )
{
	int userInput = 0;
	std::cout << "Please enter an integer to add to the vector: " ;
	std::cin >> userInput;

	vec.push_back(userInput) ;
}

void RemVector( std::vector<int>& vec )
{
    if( !vec.empty() ) vec.pop_back() ;
}
You are confusing vectors being passed by reference as vectors passed by pointer. Look at what JLBorges did in his code snippets.

http://www.cplusplus.com/articles/ENywvCM9/

Passing by pointer would look like this: void DisplayVector(const vector<T>* inVec)

and require you to change your function calls in main() to: DisplayVector(&integers);
hmmm ok, thanks, I will play around some more. Everytime I think I understand pointers and references I find something new that I did not understand.

also, yup I forgot to validate my input before removing, thanks for that.
Some people insist having vectors store pointers, without realizing they can get pointers with a vector's iterators.

Everytime I think I understand pointers and references I find something new that I did not understand.

Been there, done that.

Still there for some of the more "arcane" C++ features. ;)

Using a range-based for loop makes walking-through/printing out a vector really easy:
1
2
3
4
5
6
7
8
9
10
template <typename T>
void DisplayVector(const std::vector<T>& vec)
{
   for (const auto& itr : vec)
   {
      std::cout << itr << ' ';
   }

   std::cout << '\n';
}


You have DisplayVector() already templated, why not go an extra step and overload operator<< so printing out a vector is as easy as: std::cout << vec << '\n';

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

template <typename T>
void DisplayVector(const std::vector<T>&);

template <typename T>
std::ostream& operator<<(std::ostream&, const std::vector<T>&);

int main()
{
   std::vector<int> vec { 1, 2, 3, 4, 5, 6 };

   DisplayVector(vec);

   // overloading operator<< makes printing out a vector easy
   std::cout << vec << '\n';
}

template <typename T>
void DisplayVector(const std::vector<T>& vec)
{
   for (const auto& itr : vec)
   {
      std::cout << itr << ' ';
   }

   std::cout << '\n';
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
   for (auto const& x : vec)
   {
      os << x << ' ';
   }

   return os;
}

1 2 3 4 5 6
1 2 3 4 5 6

Some people insist having vectors store pointers, without realizing they can get pointers with a vector's iterators.

A vector of pointers is useful in other contexts. An example is a vector of pointers to a sorted version of unsorted data. Only the pointers are rearranged by the sorting, not all the data -- useful for performance or if you needed the original data in place and a sorted copy of it to display or something.

Topic archived. No new replies allowed.