undefined reference help

I am doing code for my class at school and i can not seem to figure out this problem. I have never had any problems with classes and including header files to them up until this one.

Here is my Header file which is given by our teacher and shouldn't be touched.
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
  /* @file: intbox.h
 * Definition of class IntBox
 * @C - Galaxy Express Softworks 
 * 
 * Version 14.3.0
 
 */

#ifndef INTBOX_H
#define INTBOX_H
#include <iostream>
using namespace std;


/* _________________
  /                 \
  |  IntBox  Class  |
  \_________________/
   
    Purpose: Implements an Integer Box, as required by the Emperor Lrrr

    An Integer-Box is a class that encapsulates an array of Integers
    The size of the array is specified at declaration.

*/

// ---------------  Class IntBox Declarations
class IntBox
{ 
public:  
  int* m_ints;                        // Array of Integers
  int m_boxsize;                      // number of Integers in this box       


public:
  // Purpose: Constructs an Integer Box
	// Preconditions:
	//     's' is greater than 0;
  // Postconditions: 
  //     m_ints points to a dynamically allocated array of size 's'
  //     all elements of m_ints[] are initiallized to 'a'.
  
  IntBox(int s, int a);


  /* 
   * --------- Big 3 Member Functions -----------
   */

  // Purpose: Destructor
  // Postconditions: m_ints[] deallocated
  ~IntBox();

  // Purpose: Operator=, performs a deep copy of 'rhs' into 'this' IntBox
  // Parameters: rhs, IntBox to be copied
  // Returns: *this
  // Postconditions: *this == rhs
  const IntBox& operator=(const IntBox& rhs);

  // Purpose: Copy Constructor
  // Parameters: rhs - IntBox to be copied
  // Postconditions:  *this == rhs
  IntBox(const IntBox& rhs);


  /* 
   * ----- Simple Accessor Operations ----- 
   */

  // Purpose: Sets a value in the IntBox
  // Parameters: 'i' location to set
  //             'x' value to store
  // PreConditions: 'i' is between the boundaries of the IntBox 
  // Postconditions:  element 'i' in the IntBox is set to 'x'
	void set( int i, int x);


  /* 
   * ----- Complex Accessor Operations ----- 
   */
  
  // Purpose: prints the IntBox
  friend std::ostream& operator<< (std::ostream& out, const IntBox& box);
}; // IntBox
#endif 


Now here is the .cpp file i created for the class:
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
//intbox.h

#include "intbox.h"
#include <iostream>

using namespace std;


IntBox::IntBox(int s, int a)
{
	//Initiallizing new array
	m_ints = new int[s];
	m_boxsize = s;
	
	//Fill it with the int a
	for(int k=0; k<s; k++)
	{
		m_ints[k] = a;
	}
}


IntBox::~IntBox()
{
	delete []m_ints;
}


const IntBox& IntBox::operator=(const IntBox& rhs)
{
	if(this != &rhs)
	{	
		//deallocate m_ints before copying it
		delete []m_ints;
		//set size equal to new size
		m_boxsize = rhs.m_boxsize;
		//create a new array with its new size
		m_ints = new int[m_boxsize];
		//copy over data
		for(int k=0; k<m_boxsize; k++)
		{
			m_ints[k] = rhs.m_ints[k];
		}
	}
	//return the new Array
	return *this;
}


IntBox::IntBox(const IntBox& rhs)
{
	if(this != &rhs)
	{
		//set size equal to new size
		m_boxsize = rhs.m_boxsize;
		//create a new array with its new size
		m_ints = new int[m_boxsize];
		//copy over data
		for(int k=0; k<m_boxsize; k++)
		{
			m_ints[k] = rhs.m_ints[k];
		}
	}
}


void IntBox::set( int i, int x)
{
	m_ints[i] = x;
}



std::ostream& operator<< (std::ostream& out, const IntBox& box)
{
	out<<"[ ";
	
	for(int k=0; k<box.m_boxsize; k++)
	{
		out<< box.m_ints[k] << ", ";
	}
	out<<"]";
	
	return out;
}


And finally here is the tester.cpp that the teacher gave us to test if our class implementation was working

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
#include "intbox.h"
#include <iostream>

using namespace std;

int main () 
{
	cout << "IntBox Tester" << endl;
	
	cout << "#1" << endl;
	IntBox a(6,2);
	cout << a << endl;

	cout << "#2" << endl;
	IntBox b(4,2);
	cout << b << endl;
	
	cout << "#3" << endl;
	IntBox c(5,5);
	c.set( 1, 77 );
	cout << c << endl;
	
	cout << "#4" << endl;
	IntBox d(8,8);
	d.set( 2, 1 );
	cout << d << endl;

	cout << "#5 : Operator=" << endl;
	c = d;
	cout << d << endl;
	cout << c << endl;
	c.set( 5, 42 );
	cout << d << endl;
	cout << c << endl;
	
	cout << "#6 : Copy Constructor" << endl;
	IntBox e(a);
	cout << a << endl;
	cout << e << endl;
	e.set( 3, 88 );
	cout << a << endl;
	cout << e << endl;
	
  
  return 0;
}


Whenever i compile the TESTER.cpp i get a bunch of errors like these:
tester.cpp:(.text+0x6a): undefined reference to `IntBox::IntBox(int, int)'
tester.cpp:(.text+0x7d): undefined reference to `operator<<(std::ostream&, IntBox const&)'

I am not sure what i am doing wrong. Thank you for any help!
I could really use some help as i am very very confused and my project is due later tomorrow.
closed account (48T7M4Gy)
Try putting all the files in the same directory and you will get your program to run. :)
They are all in the same folder! i think my compiler is having an issue... but any more advice would be helpful!
closed account (48T7M4Gy)
I am using VS2013 and all works well. Maybe clean and rebuild?
any other programs you know of that come with a compiler with it? i was using Dev c++.
closed account (48T7M4Gy)
Code Blocks is not bad - free
Visual Studio - free
I am trying to grab VS really quick. Thanks again for all of your help!
No luck. i haven't the slightest idea what is wrong.
closed account (48T7M4Gy)
FWIW I just used Dev C++ for the first time so I can't be sure what you are doing. However your code does run in Dev C++

What I did was create a new blank project. I called it Project1
Then I dragged the 3 files into the project via the project tab.
Compile and run.
Then open Project1.exe via cmd.exe

It all works the same. For a small download Dev C++ looks OK. :)


PS If you decide to use VS you have to do the same as above.
Last edited on
Alright so i followed your steps, and it does not compile correctly in Dev C++.

When i compile intbox.cpp i get:
In function `main':
undefined reference to `WinMain'
[Error] ld returned 1 exit status

And then when i compile the tester i get huge list of undefined references.

-Thanks again for all our help
ALSO ALSO i just installed Dev c++ onto my other computer and compiled and still got the same errors so this is a programming mistake i think.
It is definitely a tool chain issue.

Make sure all of the files are included in your project. You should not need to compile source files individually. Should look something like:
http://s14.postimg.org/o6w0zv6xd/CB_Int_Box.png


When i compile intbox.cpp i get:
In function `main':
undefined reference to `WinMain'
[Error] ld returned 1 exit status

This suggests you've started the wrong type of project. Begin with an Empty Project.
@Cire

Thank you thank you very much. It was a chain issue, i never knew you had to create a project file to chain them all together but that makes sense. It complies and runs correctly.

Thanks you a bunch, you saved my homework and my sanity!
Topic archived. No new replies allowed.