#ifndef _SAMPLE_
#define _SAMPLE_
#include <cstddef>
usingnamespace std;
template <typename Object>
class Sample {
public:
explicit Sample(const Object & value = Object()) : size_{0} { //constructor for Sample
array_ = new Object{value};
}
Sample(size_t num) : array_{new Object[num]}, size_{num} { } //loaded constructor for temp
Sample(const Sample & rhs) : size_{rhs.size_} {
array_ = new Object[size_];
for (unsignedint i = 0; i < size_; i++) {
array_[i] = rhs.array_[i];
}
}
/*copy constructor
@param: const Sample & rhs
@pre: rhs.size_ and rhs.array_ are valid
@post: sets the new array and size to rhs' array and size
no return
*/
Sample & operator=(const Sample & rhs);
/*copy assignment operator
@param: const Sample & rhs
@pre: rhs.size_ and rhs.array_ are valid
@post: swaps a newly created temp's size and array with array_ and size_
return *this
*/
Sample(Sample && rhs) : array_{rhs.array_}, size_{rhs.size_} {
rhs.array_ = nullptr;
rhs.size_ = 0;
}
/*move constructor
@param: Sample && rhs
@pre: rhs.size_ and rhs.array_ are valid
@post: sets array_ to rhs' array and size_ to rhs' size, sets rhs' array and size to null (or 0)
no return
*/
Sample & operator=(Sample && rhs);
/*move assignment operator
@param: const Sample && rhs
@pre: rhs.size_ and rhs.array_ are valid
@post: swaps size_ with rhs' size and array_ with rhs' array
return *this
*/
void print(ostream & out) const;
/*print function (paired with overloaded << operator)
@param: ostream & out
@pre: none
@post: prints contents of array_
no return
*/
void ReadSample();
/*read function (reads input from user)
@param: none
@pre: user enters valid size and contents for array
@post: size is set and array is filled
no return
*/
size_t Size() const;
/*Size function (returns the size of the array)
@param: none
@pre: none
@post: prints the size of the array
return size_
*/
Sample & operator+=(const Sample & rhs);
/*overloaded += operator (paired with overloaded + operator for adding two Samples)
@param: const Sample & rhs
@pre: rhs.size_ and rhs.array_ are valid
@post: fills a temp Sample with rhs' size and array values
return *this
*/
Sample operator+(const Sample & rhs) const;
/*overloaded + operator (paired with overloaded += operator for adding two Samples)
@param: const Sample & rhs
@pre: rhs.size_ and rhs.array_ are valid, temp is valid
@post: prepares temp to be returned (combination of two Samples)
return temp
*/
Sample operator+(const Object & rhs) const;
/*overloaded + operator (to add string literal to the end of a Sample)
@param: const Object & rhs
@pre: rhs.size_ and rhs.array_ are valid, temp is valid
@post: prepares temp to be returned (combination of a Sample + string literal)
return temp
*/
Object & operator[](int num) const;
/*overloaded [] operator
@param: int num
@pre: num is valid, mustn't be greater than the array's size or less than 0
@post: none
return array_[num]
*/
~Sample(); //destructor (destroys array_)
private:
Object *array_; //dynamic array
size_t size_; //size of array
};
#endif //_SAMPLE_
#include <iostream>
#include <string>
#include <cstdlib>
#include "Sample.h"
usingnamespace std;
template <typename Object>
Sample<Object> &Sample<Object>::operator=(const Sample<Object> & rhs) { //copy assignment operator
if (this != &rhs) {
Sample temp(rhs); //create a temp to hold rhs
std::swap(size_, temp.size_); //swap size_ and temp's size
std::swap(array_, temp.array_); //swap array_ with temp's array
}
return *this; //return rhs
}
template <typename Object>
Sample<Object> &Sample<Object>::operator=(Sample<Object> && rhs) { //move assignment operator
if (this != &rhs) {
std::swap(size_, rhs.size_); //swap size_ with rhs' size
std::swap(array_, rhs.array_); //swap array_ with rhs' array
}
return *this; //return rhs
}
template <typename Object>
void Sample<Object>::print(ostream & out) const { //print function used in overloaded << operator
for (unsignedint i = 0; i < size_; i++) {
out << array_[i] << " "; //print the contents of the array
}
}
template <typename Object>
void Sample<Object>::ReadSample() {
int count = 0;
cout << "Enter a size: ";
cin >> size_; //user enters desired size
array_ = new Object[size_]; //create a new dynamic array of size "size_"
for (unsignedint i = 0; i < size_; i++) {
cout << "Enter element " << count + 1 << ": "; //user enters each individual element
cin >> array_[i];
count++;
}
}
template <typename Object>
size_t Sample<Object>::Size() const {
return size_; //returns the size of the array
}
template <typename Object>
Sample<Object> &Sample<Object>::operator+=(const Sample<Object> & rhs) { //overloaded += operator
Sample temp(size_ + rhs.size_); //create temp of size "size_ + rhs.size_" (in main: a's size + b's size)
for (unsignedint i = 0; i < size_; i++) {
temp.array_[i] = array_[i]; //set first portion of temp's array to a's array
}
int j = 0;
for (unsignedint k = size_; k < size_ + rhs.size_; k++, j++) {
temp.array_[k] = rhs.array_[j]; //set the remainder of temp's array to b's array
}
*this = temp; //set *this to temp
return *this; //return temp (combination of two Samples)
}
template <typename Object>
Sample<Object> Sample<Object>::operator+(const Sample<Object> & rhs) const { //overloaded to add two Samples
Sample temp(*this); //create a temp to hold rhs
temp += rhs; //add temp to rhs and set it equal to temp
return temp; //return the temp (should be combination of two Samples)
}
template <typename Object>
Sample<Object> Sample<Object>::operator+(const Object & rhs) const { //overloaded to add literal string
Sample temp(size_ + 1); //create temp and set its size to size_ + 1 (to hold new literal string value)
unsignedint i;
for (i = 0; i < size_; i++) {
temp.array_[i] = array_[i]; //set temp equal to rhs' array_
}
temp.array_[i] = rhs; //the last value of temp's array is equal to rhs (the string literal)
return temp; //return the altered Sample (should have the string literal as its last value)
}
template <typename Object>
Object &Sample<Object>::operator[](int num) const{
int size = size_; //sets size's value to size_ (to avoid unsigned vs. signed warning in compiler)
if (num < 0 || num > size) {
cout << "Index is out of range, terminating program." << endl;
exit(1); //exit the program if the inputted index is either smaller than 0 or greater than size
}
return array_[num]; //return array_'s value at num
}
template <typename Object>
Sample<Object>::~Sample<Object>() { //destructor
delete[] array_; //delete the dynamic array
}
I'm having an issue running my Makefile. All of my files are in the same folder and when I type 'make all' into the Windows command prompt (I installed Cygwin to allow Linux commands), I receive this error:
undefined reference to 'WinMain@16', error: ld returned 1 exit status, recipe for target 'TestSample' failed, ***[TestSample] Error 1, recipe for target 'all' failed, *** [all] Error 2
The 'make all' command is creating a TestSample.o object file but that's it. I am not allowed to alter the Makefile. Perhaps there's something wrong with my Sample.cpp file and the way I set up the templates?
It looks like your project was somehow mis-configured to be a Windows project, as opposed to a console project, since it's expecting a WinMain function, but cannot find one.
I don't know what you're using, but under Visual Studio 2013, go to:
Project > Properties > Linker > System > SubSystem