Undefined reference to function that is defined


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.
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
//File: lab6.cpp
#include <iostream>
#include <string>
#include <vector>
#include "Valorant.h"
using namespace 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; 
  }
}


Thanks!
1
2
void printVector(vector< Valorant > &list); //declaration (line 17)
void printVector(vector< Valorant > list){ //definition (line 59) 
¿do you see the difference?
Yea I do... I'm not sure how I missed that. But I am now getting a completely different problem where my printVector function isn't working.
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
}
Last edited on
 
void fillVector(vector< Valorant > list);


should be:

 
void fillVector(vector< Valorant >& list);


and

 
void printVector(vector< Valorant > &list);


should be:

 
void printVector(const vector< Valorant > &list);


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.
Topic archived. No new replies allowed.