I have been getting an error from my code that says undefined reference to "`printVector(std::vector<Valorant, std::allocator<Valorant> >&)'
collect2: error: ld returned 1 exit status" even though in my code the printVector function is clearly defined. The following is the code for the lab6 file. There are two other file for the valorant class but I don't think they are related to the issue.
//File: lab6.cpp
#include <iostream>
#include <string>
#include <vector>
#include "Valorant.h"
usingnamespace std;
//fillVector prototype here:
//Desc: Prompts the user with the number of characters they want to enter and adds each character to the vector
//Pre-condition: Accepts a vector
//Post-condition: Updates the vector passed to it
void fillVector(vector< Valorant > list);
//printVector
//Desc: Prints the contents of the vector (without using an iterator)
//Pre-condition: Accepts a vector (but don't make a copy of it)
//Post-condition: Displays information about the characters in the vector
void printVector(vector< Valorant > &list);
int main()
{
//Vector that stores the user input
vector< Valorant > list;
//fills the vector
fillVector(list);
//prints the vector
printVector(list);
cout << endl;
return 0;
}
//Insert fillVector here - we want to change the vector permanently!
void fillVector(vector< Valorant > list){
int addCharacters;
cout << "Please enter the number of characters (as an integer) you wish to add: " << endl;
cin >> addCharacters;
for(int i = 0; i < addCharacters; i++){
Valorant newValorant;
string charName;
int charHP;
bool charSide;
cout << "Character " << i << " name: " << endl;
cin >> charName;
newValorant.SetName(charName);
cout << "Character " << i << " HP: " << endl;
cin >> charHP;
newValorant.SetHP(charHP);
cout << "Is Character " << i << "a defender? (0 or 1): " << endl;
cin >> charSide;
newValorant.SetSide(charSide);
list.push_back(newValorant);
}
}
//Insert printVector - we do not want to make a copy of the vector
void printVector(vector< Valorant > list){
int vSize;
vSize = (int) list.size();
for(int i = 0; i < vSize; i++){
cout << list[i].GetName() << " is on team " << list[i].GetSide() << " and has " << list[i].GetHP() << " HP." << endl;
}
}
You see the difference as ne555 pointed out, but do you understand what the difference is -- what that & symbol is doing?
By default in C++, all arguments are passed by value into a function. That means that a copy of the object is passed in, and the original object outside of the function is left unchanged.
To actually modify the argument itself, you need to pass the object by reference, which is what the & symbol does in this context.
1 2 3 4
void func(int a)
{
a = a + 42; // this is only changing a local copy, a. No effect on the rest of the program.
}
1 2 3 4
void func(int& a)
{
a = a + 42; // this changes the actual variable that was passed into func
}
as printVector shouldn't change the contents of the vector.
When using dynamic memory containers (such as vector), they should almost always be passed by ref so that a copy is not performed (as is done using default copy by value). When the data shouldn't be changed, then the argument should be const ref.