-
undefined reference
Hey! When trying to compile the code for a ordered vector class I get the following error:
[Linker error] undefined reference to `WinMain@16'
Anyone have any idea what I might be doing wrong? This is driving me crazy and any help would be greatly appreciated.
Code is as follows:
Code:
#include <stdlib.h>
#include <iostream>
#ifndef _ORDEREDVECTOR_H
#define _ORDEREDVECTOR_H
template <class Object>
class orderedVector
{
public:
orderedVector(int n);
orderedVector( );
orderedVector(const orderedVector & v);
virtual ~orderedVector( );
void dissolve ( );
void add (const Object x);
void remove (const Object x);
void removeAt (int i);
Object & operator [](int i);
int length( ) const;
bool isEmpty( ) const;
bool contains(const Object x);
private:
Object * buffer;
int size, capacity;
void reserve(int newCapacity);
void resize(int newCap) {reserve(newCap);}
};
template <class Object>
orderedVector<Object>::orderedVector( ) :size(0),capacity(0),buffer(0){}
template <class Object>
orderedVector<Object>::orderedVector(int n): size(0), capacity(n), buffer(0)
{
if (n < 0)
{
cerr << "Error: You can't create a vector with a negative capacity!\n";
exit(1);
}
}
template <class Object>
orderedVector<Object>::orderedVector(const orderedVector & v)
{
if (this == &v)
{
cerr << "Error: You can't copy a vector onto itself!\n";
exit(1);
}
buffer = 0;
buffer = new Object[v.capacity];
size = v.length( );
capacity = v.capacity;
for (int i = 0; i < v.length( ); i++)
{
buffer[i] = v.buffer[i];
}
}
template <class Object>
orderedVector<Object>::~orderedVector( )
{
delete [ ] buffer;
}
template <class Object>
void orderedVector<Object>::add(const Object x)
{
if (size == capacity)
resize(capacity + 5);
if (size == 0)
buffer[0] = x;
else
{
for (int i = 0; i < size; i++)
{
if (x <= buffer[i])
{
int j = size;
while (j > i)
{
buffer[j] = buffer[j-1];
j--;
}
buffer[i] = x;
}
}
}
size++;
}
template <class Object>
void orderedVector<Object>::remove(const Object x)
{
bool temp;
for (i = 0; i < size; i++)
{
if (buffer[i] == x)
temp = true;
else
temp = false;
}
if (temp == false)
{
cerr << "Error: Object doesn't exist!\n";
exit(1);
}
else
{
for (int i = 0; i < size; i++)
{
int j = i;
while (j < size - 1)
{
buffer[j] = buffer[j+1];
j++;
}
}
size--;
}
}
template <class Object>
void orderedVector<Object>::removeAt(const int i)
{
if (i < 0 || i >= size)
{
cerr << "Error: Index out of range!\n";
exit(1);
}
int j = i;
while (j < size - 1)
{
buffer[j] = buffer[j+1];
j++;
}
size--;
}
template <class Object>
Object & orderedVector<Object>::operator [] (int i)
{
if (i < 0 || i >= size)
{
cerr << "Error: Index out of range!\n";
exit(1);
}
return buffer[i];
}
template <class Object>
int orderedVector<Object>::length( ) const
{
return size;
}
template <class Object>
bool orderedVector<Object>::isEmpty( ) const
{
return size == 0;
}
template <class Object>
bool orderedVector<Object>::contains(const Object x)
{
bool temp;
for (i = 0; i < size; i++)
{
if (buffer[i] == x)
temp = true;
else
temp = false;
}
return temp;
}
template <class Object>
void orderedVector<Object>::reserve(int newCap)
{
if (buffer == 0)
{
size = 0;
capacity = 0;
}
if (newCap <= capacity)
return;
Object * newBuffer = new Object[newCap];
for (int i = 0; i < size; i++)
newBuffer[i]=buffer[i];
capacity = newCap;
delete [ ] buffer;
buffer = newBuffer;
}
#endif
-
You're not really doing anything wrong. In fact, if you get a linker error your code's (probably) compiling fine, but a linker is meant to make a working executable file. Since this is just part of a program, you can't compile it any further than an object file. In other words, if you want to stop getting that error, either build a program around that class (using a main or WinMain function) or disable linking in your IDE. (assuming you're using an IDE)
If you want to make this into a library for later use, you'll have to do some other stuff.
-
You're using a compiler that targets windows, and is configured to create a windows application (for which the starting function is named something like winmain() rather than main()).
Look into configuration options for your compiler, and look for a setting concerned with creating console applications. A console application is essentially one that starts with main() rather than winmain().
That is a generic description. If youy identify your compiler (and version) someone may be able to give you more specific hints to fix your problem.
-
I'm using Bloodshed Dev-C++, version 4.9.9.0
-
Then use:
Execute -> Compile
Or the compile button which is the four little boxes icon.
-
I am running the compile, thats when the error shows up under messges.
-
Tools -> Compiler Options
Check the box that says "Add the following commands when calling compiler:"
then inside the big box put