Undefined reference error

I am trying to implement an "audio" object that will hold a patient's first name, last name and dob.

I'm am trying to initialize an instance of the object, which I happen to name 'the patient' in the main file, included below, but I get a compile error. This is the error: /tmp/ccHKkLmo.o: In function `main':
theaudio.cpp:(.text+0x131): undefined reference to `Audio::Audio(std::string, std::string, std::string)'
collect2: error: ld returned 1 exit status

I am lost. Any help would be much appreciated.


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
115
116
117
118
119
120
121
122
123
  HEADER FILE - audio.h
 -----------#include <iostream>
#include <string>
#ifndef AUDIO_H
#define AUDIO_H

class Audio
{
private:
 int l_8000hz;
 int l_6000hz;
 int l_4000hz;
 int l_3000hz;
 int l_2000hz;
 int l_1000hz;
 int l_500hz;
 int l_250hz;
 int r_8000hz;
 int r_6000hz;
 int r_4000hz;
 int r_3000hz;
 int r_2000hz;
 int r_1000hz;
 int r_500hz;
 int r_250hz;
 std::string patient_fName;
 std::string patient_lName;
 std::string patient_dob;


public:
 //Audio();
 Audio(std::string patient_fName, std::string patient_lName, std::string patient_dob);
 void SetAudio(std::string patient_fName, std::string patient_lName, std::string patient_dob);
 
 std::string Get_fName() { return patient_fName; }
 std::string Get_lName() { return patient_lName; }
 std::string Get_dob() { return patient_dob; }
 int Get_l_8000hz() { return l_8000hz; }
 int Get_l_6000hz() { return l_6000hz; }
 int Get_l_4000hz() { return l_4000hz; }
 int Get_l_3000hz() { return l_3000hz; }
 int Get_l_2000hz() { return l_2000hz; }
 int Get_l_1000hz() { return l_1000hz; }
 int Get_l_500hz() { return l_500hz; }
 int Get_l_250hz() { return l_250hz; }
 int Get_r_8000hz() { return r_8000hz; }
 int Get_r_6000hz() { return r_6000hz; }
 int Get_r_4000hz() { return r_4000hz; }
 int Get_r_3000hz() { return r_3000hz; }
 int Get_r_2000hz() { return r_2000hz; }
 int Get_r_1000hz() { return r_1000hz; }
 int Get_r_500hz() { return r_500hz; }
 int Get_r_250hz() { return r_250hz; }
 void Set_l_8000hz(int a){ l_8000hz =a; } 
 void Set_l_6000hz(int b) {l_6000hz = b;}  
 void Set_l_4000hz(int c){l_4000hz = c; }
 void Set_l_3000hz(int d){l_3000hz = d; }
 void Set_l_2000hz(int e){l_2000hz = e; }
 void Set_l_1000hz(int f){l_1000hz = f; }
 void Set_l_500hz(int g){l_500hz = g; }
 void Set_l_250hz(int h){l_250hz = h; }
 void Set_r_8000hz(int i){r_8000hz = i; }
 void Set_r_6000hz(int j){r_6000hz = j; }
 void Set_r_4000hz(int k){r_4000hz = k; }
 void Set_r_3000hz(int l){r_3000hz = l; }
 void Set_r_2000hz(int m){r_2000hz = m; }
 void Set_r_1000hz(int n){r_1000hz = n; }
 void Set_r_500hz(int o){r_500hz = o; }
 void Set_r_250hz(int p) {r_250hz = p; }

};

#endif
  ------------------------
  CPP FILE - audio.cpp
  ------------------------
#include "audio.h"
#include <iostream>
#include <string>

// Audio constructor
Audio::Audio(string cpatient_fName, string cpatient_lName, string cpatient_dob)
{
 cpatient_fName = patient_fName;
 cpatient_lName = patient_lName;
 cpatient_dob = patient_dob;
}

//void Audio::SetAudio(string cpatient_fName, string cpatient_lName, string cpatient_dob)
//{
// cpatient_fName = patient_fName;
// cpatient_lName = patient_lName;
// cpatient_dob = patient_dob;
//}

   -------------------------------
     MAIN FILE - theaudio.cpp
   -------------------------------
#include "audio.h"
#include <iostream>

using namespace std;

int main()
{
 string firstName, lastName, dob;
 cout << "Enter patient last name: ";
 cin >> lastName;
 cout << "\n";
 cout << "Enter patient first name: ";
 cin >> firstName;
 cout << "\n";
 cout << "Enter patient's date of birth";
 cin >> dob;
 cout << "\n";
 Audio patient(firstName, lastName, dob);

 return 0;

}

How are you compiling? Via the command line? You must link the different files into the same executable. I suggest you use an IDE where you add this code into a project; the IDE will link it for you.
You've also got a problem in your constructor at lines 85-87. Your assignments are reversed. cpatient_xx is the argument being passed in. patient_xxx is the member of the class. It should be:
1
2
3
4
5
6
Audio::Audio(string cpatient_fName, string cpatient_lName, string cpatient_dob)
{
  patient_fName = cpatient_fName;
  patient_lName = cpatient_lName;
  patient_dob = cpatient_dob;
}



Thanks: Kefin & AbstractionAnon.

I am compiling from the command line on Ubuntu. I downloaded an IDE and got the same error, unfortunately.

Do either of you know where I can find the syntax structure for compiling with c++ from the command line?
Last edited on
I believe this is compiler dependent.

As a side note I suggest you use Code::Blocks, it is light, cross-platform and easy to use but can be elaborate as well. Comes with debugging tools built in.
Last edited on
If he's on Ubuntu he is very likely using the GCC.

Files:

main.cpp
foo.hpp
foo.cpp

Compile with:

g++ main.cpp foo.cpp -o smile

Now you have 'smile' program which you can run.

Alternate compile method:

g++ -c main.cpp
g++ -c foo.cpp
g++ main.o foo.o -o smile

This way, you only need recompile parts of your program that actually need it.. This is more useful in large projects.

Hope this helps.
One other problem is that in his audio.cpp file he didn't do std::string. Here is his code after I got it working:
audio.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef AUDIO_H
#define AUDIO_H
#include <iostream>
#include <string>

class Audio
{
private:
 int l_8000hz;
 int l_6000hz;
 int l_4000hz;
 int l_3000hz;
 int l_2000hz;
 int l_1000hz;
 int l_500hz;
 int l_250hz;
 int r_8000hz;
 int r_6000hz;
 int r_4000hz;
 int r_3000hz;
 int r_2000hz;
 int r_1000hz;
 int r_500hz;
 int r_250hz;
 std::string patient_fName;
 std::string patient_lName;
 std::string patient_dob;


public:
 //Audio();
 Audio(std::string patient_fName, std::string patient_lName, std::string patient_dob);
 void SetAudio(std::string patient_fName, std::string patient_lName, std::string patient_dob);
 
 std::string Get_fName() { return patient_fName; }
 std::string Get_lName() { return patient_lName; }
 std::string Get_dob() { return patient_dob; }
 int Get_l_8000hz() { return l_8000hz; }
 int Get_l_6000hz() { return l_6000hz; }
 int Get_l_4000hz() { return l_4000hz; }
 int Get_l_3000hz() { return l_3000hz; }
 int Get_l_2000hz() { return l_2000hz; }
 int Get_l_1000hz() { return l_1000hz; }
 int Get_l_500hz() { return l_500hz; }
 int Get_l_250hz() { return l_250hz; }
 int Get_r_8000hz() { return r_8000hz; }
 int Get_r_6000hz() { return r_6000hz; }
 int Get_r_4000hz() { return r_4000hz; }
 int Get_r_3000hz() { return r_3000hz; }
 int Get_r_2000hz() { return r_2000hz; }
 int Get_r_1000hz() { return r_1000hz; }
 int Get_r_500hz() { return r_500hz; }
 int Get_r_250hz() { return r_250hz; }
 void Set_l_8000hz(int a){ l_8000hz =a; } 
 void Set_l_6000hz(int b) {l_6000hz = b;}  
 void Set_l_4000hz(int c){l_4000hz = c; }
 void Set_l_3000hz(int d){l_3000hz = d; }
 void Set_l_2000hz(int e){l_2000hz = e; }
 void Set_l_1000hz(int f){l_1000hz = f; }
 void Set_l_500hz(int g){l_500hz = g; }
 void Set_l_250hz(int h){l_250hz = h; }
 void Set_r_8000hz(int i){r_8000hz = i; }
 void Set_r_6000hz(int j){r_6000hz = j; }
 void Set_r_4000hz(int k){r_4000hz = k; }
 void Set_r_3000hz(int l){r_3000hz = l; }
 void Set_r_2000hz(int m){r_2000hz = m; }
 void Set_r_1000hz(int n){r_1000hz = n; }
 void Set_r_500hz(int o){r_500hz = o; }
 void Set_r_250hz(int p) {r_250hz = p; }

};

#endif 

audio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "audio.h"
#include <iostream>
#include <string>

// Audio constructor
Audio::Audio(std::string cpatient_fName, std::string cpatient_lName, std::string cpatient_dob)
{
 patient_fName = cpatient_fName;
 patient_lName = cpatient_lName;
 patient_dob = cpatient_dob;
}

//void Audio::SetAudio(string cpatient_fName, string cpatient_lName, string cpatient_dob)
//{
// cpatient_fName = patient_fName;
// cpatient_lName = patient_lName;
// cpatient_dob = patient_dob;
//} 

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "audio.h"
#include <iostream>

using namespace std;

int main()
{
 string firstName, lastName, dob;
 cout << "Enter patient last name: ";
 cin >> lastName;
 cout << "\n";
 cout << "Enter patient first name: ";
 cin >> firstName;
 cout << "\n";
 cout << "Enter patient's date of birth";
 cin >> dob;
 cout << "\n";
 Audio patient(firstName, lastName, dob);

 return 0;

}


To compile by command I did:

g++ -c audio.cpp
g++ -o audio main.cpp audio.o

Output was:

Enter patient last name: Specter

Enter patient first name: BHX

Enter patient's date of birth1/1/1

Didn't mess with fixing the typo of not having ':' after birth.
I know hindsight is a wonderful thing, but I mention these things to make it easier for respondents, and to provide some info for others.

Given all the errors identified so far, It seems to me that that the OP should have posted all the compile errors, not just one, and specified more detail about the compiler & system being used.

People who reply either have to have an eagle-eye when doing in-brain compiling, or they compile it themselves. Much easier to look at the compile errors posted by the OP.

One should set the compiler warnings to their maximum, on g++ :

-Wall -Wextra -pedantic

And to be really thorough, promote all warnings to errors with -Werror

Also, if you like, set the C++ 11 standard

-std=c++11

or

-std=gnu++11

for the gnu extensions.

HTH
Thanks much, BHXSpector!

Your code & compile directions worked.

Thanks TheIdeasMan for the advice.
Last edited on
Topic archived. No new replies allowed.