Undefined reference to 'WinMain@16'.

Sample.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
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
#ifndef _SAMPLE_
#define _SAMPLE_
#include <cstddef>

using namespace 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 (unsigned int 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_ 


Sample.cpp:
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
#include <iostream>
#include <string>
#include <cstdlib>

#include "Sample.h"

using namespace 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 (unsigned int 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 (unsigned int 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 (unsigned int 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 (unsigned int 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)

	unsigned int 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
}


main.cpp:
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
#include <iostream>
#include <string>
#include <cstdlib>

#include "Sample.h"
#include "TestSample.cpp"

using namespace std;

void TestPart1();
void TestPart2();

template <typename Object>
ostream & operator<<(ostream & out, const Sample<Object> & rhs) {
	rhs.print(out);
	return out;
}

int main(int argc, char **argv) {
   TestPart1();
   TestPart2();
  return 0;
}

void TestPart1() {
	cout << "---PART 1---" << endl;
	Sample<int> a, b; //Two empty Samples
	cout << a.Size() << " " << b.Size() << endl; //yields 0 0

	Sample<int> d{7}; //Sample containing 7 should be created
	cout << d << endl;

	a.ReadSample(); //user enters a Sample
	cout << a << endl; //output should be what user entered

	b.ReadSample();
	cout << b << endl;

	Sample<int> c{a}; //copy constructor
	cout << c << endl;
	cout << a << endl;

	a = b; //copy assignment operator
	cout << a << endl;

	Sample<int> e = std::move(c); //move constructor
	cout << e << endl;
	cout << c << endl;

	a = std::move(e); //move assignment operator
	cout << a << endl;
	cout << e << endl;

} //destructor will be called

void TestPart2() {
	cout << "---PART 2---" << endl;
	Sample<string> a, b;

	a.ReadSample(); //user provides input for Sample a
	cout << a << endl;

	b.ReadSample(); //user provides input for Sample b
	cout << b << endl; 

	cout << a + b << endl; //concatenates the two Samples

	Sample<string> d = a + b;
	cout << d << endl;

	cout << d + "adding this" << endl; //adds an element to the end

	cout << a[2] << endl; //should print 3rd element, exception is thrown if it is out of range

	b[1] = "change1";
	cout << b << endl;
	b[0] = "change2";
	cout << b;
}
Last edited on
Makefile:
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
#FLAGS
C++FLAG = -g -std=c++11 -Wall

#Math Library
MATH_LIBS = -lm
EXEC_DIR=.





#Rule for .cpp files
# .SUFFIXES : .cpp.o 

.cpp.o:
	g++ $(C++FLAG) $(INCLUDES)  -c $< -o $@




#Including
INCLUDES=  -I. 


LIBS_ALL =  -L/usr/lib -L/usr/local/lib $(MATH_LIBS) 


#ZEROTH PROGRAM
ALL_OBJ0= TestSample.o
PROGRAM_0=TestSample
$(PROGRAM_0): $(ALL_OBJ0)
	g++ $(C++FLAG) -o $(EXEC_DIR)/$@ $(ALL_OBJ0) $(INCLUDES) $(LIBS_ALL)

#Compiling all

all: 	
		make $(PROGRAM_0)


#Clean obj files

clean:
	(rm -f *.o; rm -f TestSample)



(:


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
Last edited on
Topic archived. No new replies allowed.