Linking problem?

Hi again everyone!! I am having some difficulty navigating this error.

Please be aware that the compiler I am forced to use is whatever Zybooks uses, which is all online and I do not have control over how it behaves, all I can do is hit "Run" and hope it works.

I also do not have access to these .txt files nor are they visible to me.

zip_code_state.txt
state_population.txt
abbreviation_state.txt

The error is as follows:

1
2
3
4
/usr/bin/ld: /tmp/ccuCujoy.o: in function `main':
main.cpp:(.text+0x6e7): undefined reference to `StatePair<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::GetValue()'
/usr/bin/ld: main.cpp:(.text+0x7d3): undefined reference to `StatePair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::GetValue()'
collect2: error: ld returned 1 exit status 


main.cpp

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
#include<iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;

int main() {
	ifstream inFS; // File input stream
	int zip;
	int population;
	string abbrev;
	string state;
	unsigned int i;

	// ZIP code - state abbrev. pairs
	vector<StatePair <int, string>> zipCodeState;

	// state abbrev. - state name pairs
	vector<StatePair<string, string>> abbrevState;

	// state name - population pairs
	vector<StatePair<string, int>> statePopulation;

	// Fill the three ArrayLists

	// Try to open zip_code_state.txt file
	inFS.open("zip_code_state.txt");
	if (!inFS.is_open()) {
		cout << "Could not open file zip_code_state.txt." << endl;
		return 1; // 1 indicates error
	}
	while (!inFS.eof()) {
		StatePair <int, string> temp;
		inFS >> zip;
		if (!inFS.fail()) {
			temp.SetKey(zip);
		}
		inFS >> abbrev;
		if (!inFS.fail()) {
			temp.SetValue(abbrev);
		}
		zipCodeState.push_back(temp);
	}
	inFS.close();
	
   // Try to open abbreviation_state.txt file
	inFS.open("abbreviation_state.txt");
	if (!inFS.is_open()) {
		cout << "Could not open file abbreviation_state.txt." << endl;
		return 1; // 1 indicates error
	}
	while (!inFS.eof()) {
		StatePair <string, string> temp;
		inFS >> abbrev;
		if (!inFS.fail()) {
			temp.SetKey(abbrev);
		}
		getline(inFS, state); //flushes endline
		getline(inFS, state);
		state = state.substr(0, state.size()-1);
		if (!inFS.fail()) {
			temp.SetValue(state);
		}
		
		abbrevState.push_back(temp);
	}
	inFS.close();
	
	// Try to open state_population.txt file
	inFS.open("state_population.txt");
	if (!inFS.is_open()) {
		cout << "Could not open file state_population.txt." << endl;
		return 1; // 1 indicates error
	}
	while (!inFS.eof()) {
		StatePair <string, int> temp;
		getline(inFS, state);
		state = state.substr(0, state.size()-1);
		if (!inFS.fail()) {
			temp.SetKey(state);
		}
		inFS >> population;
		if (!inFS.fail()) {
			temp.SetValue(population);
		}
		getline(inFS, state); //flushes endline
		statePopulation.push_back(temp);
	}
	inFS.close();
	
	cin >> zip;

   for (i = 0; i < zipCodeState.size(); ++i) {
		// TODO: Using ZIP code, find state abbreviation
		if(zipCodeState.at(i).GetKey() == zip)
		abbrev = zipCodeState.at(i).GetValue();
	}


	for (i = 0; i < abbrevState.size(); ++i) {
		// TODO: Using state abbreviation, find state name
		if(abbrevState.at(i).GetKey() == abbrev)
		state = abbrevState.at(i).GetValue();
	}


	for (i = 0; i < statePopulation.size(); ++i) {
		// TODO: Using state name, find population. Print pair info.
		if(statePopulation.at(i).GetKey() == state)
		statePopulation.at(i).PrintInfo();
	}

}


StatePair.h

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
#ifndef STATEPAIR
#define STATEPAIR

template<typename T1, typename T2>
class StatePair {
// TODO: Define a constructor, mutators, and accessors 
//       for StatePair
	      public:
	StatePair();
	
	StatePair(T1 userKey, T2 userValue);
	
	   void SetKey(T1 newKey);
	   
	   void SetValue(T2 newVal);
	   
	T1 GetKey();
	
	T2 GetValue();
	
	
	   void PrintInfo();
	
	      private:
	
	T1 key;	
	T2 value;
// TODO: Define PrintInfo() method


};
template<typename T1, typename T2>
   StatePair<T1, T2>::StatePair() {
}
template<typename T1, typename T2>
   StatePair<T1, T2>::StatePair(T1 userKey, T2 userValue) {
      key = userKey;
      value = userValue;
}
template<typename T1, typename T2>
   void StatePair<T1, T2>::SetKey(T1 newKey) {
      key = newKey;
}
template<typename T1, typename T2>
   void StatePair<T1, T2>::SetValue(T2 newVal) {
      value = newVal;
}
template<typename T1, typename T2>
   T1 StatePair<T1, T2>::GetKey() {
      return key;
}
template<typename T1, typename T2>
   void StatePair<T1, T2>::PrintInfo() {
      std::cout << key << ": " << value << std::endl;
}
#endif 

Last edited on
You forgot to implement StatePair::GetValue(). You only declared it.
!@$%$^#%@$^*

;_; ;_; ;_; ;_; ;_; ;_; ;_; ;_;

Thank you very much
Registered users can post here. Sign in or register to post.