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.
/* @file: intbox.h
* Definition of class IntBox
* @C - Galaxy Express Softworks
*
* Version 14.3.0
*/
#ifndef INTBOX_H
#define INTBOX_H
#include <iostream>
usingnamespace 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:
#include "intbox.h"
#include <iostream>
usingnamespace 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!
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.
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.
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!