Undefined reference when converting string to int.

Hello all I keep getting a undefined reference for my string_to_int function and I have gone through it multiple times and can't seem to spot the reason it is throwing the error. Any help will be greatly apprechiated. Heres the full error:

main.cpp:(.text+0x20b): undefined reference to `string_to_int(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
main.cpp:(.text+0x2e9): undefined reference to `string_to_int(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status


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
 
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

enum Error_code { duplicate_error, not_present, success };

int string_to_int(string s);



#include "utility.h"

int string_to_int(string s){
   //removed i in front of stringstream no difference
   istringstream instr(s);
   int n;
   instr >> n;
   return n;
}



#include <iostream>
#include "utility.h"
#include "utility.h"
#include "Binary_node.h"
#include "Binary_tree.h"
#include "Search_tree.h"
int main(){
   string input = "";
  int iinput = 0;
   bool exit_now = false;
   Search_tree<int> stree;
   while(!exit_now){
      cout << endl;
      cout << "***********************" << endl;
      cout << "Menu:" << endl;
      cout << "1. Incremental Insert" << endl;
      cout << "2. Incremental Delete" << endl;
      cout << "c. Clear tree" << endl;
      cout << "p. Print tree" << endl;
      cout << "h. Print tree height" << endl;
      cout << "s. Print tree size" << endl;
      cout << "x. Exit" << endl;
      cout << "***********************" << endl;

      getline(cin, input);

      if(input == "1"){
         cout << endl;
         cout << "Enter new integer keys to insert.  Enter \"q<Enter>\" to quit." << endl;
         cout << endl;
         getline(cin, input);

         while(input != "q"){
            stree.insert(string_to_int(input));
            stree.print();
            getline(cin, input);

         }
      }
      else if(input == "2"){
         cout << endl;
         cout << "Enter integer keys to delete.  Enter \"q<Enter>\" to quit." << endl;
         cout << endl;
         getline(cin, input);
         while(input != "q"){
            stree.remove(string_to_int(input));
            stree.print();
            getline(cin, input);
         }
      }
      else if (input == "c")
         stree.clear();
      else if (input == "p")
         stree.print();
      else if (input == "h")
         cout << endl << "The height of the binary tree is " << stree.height() << endl;
      else if (input == "s")
         cout << endl << "The size (node count) of the binary tree is " << stree.size() << endl;
      else if(input == "x")
         exit_now = true;
   }
}



The main class only has these includes not sure where the others came from when I posted the code.

#include "utility.h"
#include "Binary_node.h"
#include "Binary_tree.h"
#include "Search_tree.h"
Last edited on
It's unclear from how you posted your code, if lines 1-23 are in a separate file. And if so, what that file is named and if that file is a .h file, or a .cpp file.

From the error message, it would appear, that lines 1-23 are in a separate .cpp file, but that .cpp file did not get compiled and linked with main.cpp.


Thanks for the help AbstractionAnon, sorry for the confusion. Lines 2-11 are a file called utility.h, lines 15-23 are a file called utility.cpp, and the rest is the main file. How would I link the two .cpp files?
How would I link the two .cpp files?

That depends on what compiler or IDE you're using.

Typically in an IDE you would have created a project, then added both main.cpp and utility.cpp to that project. The IDE should take care of compiling both and linking them together.
Topic archived. No new replies allowed.