I am trying to compile and run an OOP Project but I keep getting 7 LNK2019 Errors everything looks to be defined and linked together. I do not understand why it is not running as it should. I tried running the OOP project in something else besides Visual Studio like CodeBlocks but I also keep getting "Undefined Reference to...". Any help on this topic would be greatly appreciated.
client code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
#include "orderedpair.h"
using namespace std;
using namespace cs_pairs;
int main() {
int num1, num2;
OrderedPair<int> myList[10];
srand(static_cast<unsigned>(time(0)));
cout << "default value: ";
myList[0].print();
cout << endl;
for (int i = 0; i < 10; i++) {
myList[i].setFirst(rand() % 50);
myList[i].setSecond(rand() % 50 + 50);
}
myList[2] = myList[0] + myList[1];
if (myList[0] < myList[1]) {
myList[0].print();
cout << " is less than ";
myList[1].print();
cout << endl;
}
for (int i = 0; i < 10; i++) {
myList[i].print();
cout << endl;
}
cout << "Enter two numbers to use in an OrderedPair. Make sure they are different numbers: ";
cin >> num1 >> num2;
OrderedPair<int> x;
x.setFirst(num1);
x.setSecond(num2);
cout << "The resulting OrderedPair: ";
x.print();
cout << endl;
}
Header Code:
#include <iostream>
namespace cs_pairs
{
template <class T>
class OrderedPair
{
public:
//static const int DEFAULT_VALUE = int();
//inline static const T DEFAULT_VALUE = T{};
static const int DEFAULT_VALUE;
typedef std::size_t size_type;
typedef T value_type;
OrderedPair(value_type newFirst = DEFAULT_VALUE, value_type newSecond = DEFAULT_VALUE);
void setFirst(value_type newFirst);
void setSecond(value_type newSecond);
value_type getFirst() const;
value_type getSecond() const;
OrderedPair operator+(const OrderedPair& right) const;
bool operator<(const OrderedPair& right) const;
void print() const;
class DuplicateMemberError
{
};
private:
value_type first;
value_type second;
};
//template <class T>
//const int OrderedPair<T>::DEFAULT_VALUE = int();
}
Implementation Code:
#include "orderedpair.h"
#include <iostream>
using namespace std;
namespace cs_pairs {
template <class T>
OrderedPair<T>::OrderedPair(value_type newFirst , value_type newSecond) {
setFirst(newFirst);
setSecond(newSecond);
}
template <class T>
void OrderedPair<T>::setFirst(value_type newFirst) {
// if statement to throw an exception if precondition not met goes here.
first = newFirst;
}
template <class T>
void OrderedPair<T>::setSecond(value_type newSecond) {
// if statement to throw an exception if precondition not met goes here.
second = newSecond;
}
template <class T>
typename OrderedPair<T>::value_type OrderedPair<T>::getFirst() const {
return first;
}
template <class T>
typename OrderedPair<T>::value_type OrderedPair<T>::getSecond() const {
return second;
}
template <class T>
OrderedPair<T> OrderedPair<T>::operator+(const OrderedPair<T>& right) const {
return OrderedPair(first + right.first, second + right.second);
}
template <class T>
bool OrderedPair<T>::operator<(const OrderedPair<T>& right) const {
return first + second < right.first + right.second;
}
template <class T>
void OrderedPair<T>::print() const {
cout << "(" << first << ", " << second << ")";
}
}