Reputation: 4581
I am trying to re-create the vector class for my own understanding. I came across an issue recently with Visual Studio 2013 while trying to compile this code. I am getting an error for each 'vector' keyword that states:
I do not know if this is a range error within my class definition or if this is something else.I have not played with any settings.
#include "../../../std_lib_facilities.h"
class vector{
int sz;
double* elem; //pointer to the first element (of type double)
public:
vector(int s) :sz(s), //constructor - allocates s doubles , :size(s) is a 'initilization list'
elem(new double[s])
{
for (int i = 0; i < s; i++)
elem[i] = 0; //initialize elements
}
int size() const
{
return sz;
}
//read
double get(int n)
{
return elem[n];
}
//write
void set(int n, double v)
{
elem[n] = v;
}
//Every class that owns a resource needs a destructor
~vector() //destructor
{
delete[] elem; // free memory
}
};
int main(int argc)
{
vector v(5);
for (int i = 0; i < v.size(); i++){
v.set(i, i);
cout << "v[" << i << "]==" << v.get(i) << '\n';
}
system("PAUSE");
}
If you need any more information please don't hesitate to ask.
Upvotes: 0
Views: 800
Reputation: 13536
I'm assuming this is the std_lib_facilities.h
you have included. That file contains a macro
// disgusting macro hack to get a range checked vector:
#define vector Vector
This causes all following occurrence of vector
in your code to be replaced by Vector
. So when you declare your class vector
it gets changed to class Vector
, and since the header you have included already declares a template type named Vector
, you probably get a redefined symbol error. In addition, in main when you declare vector v(5);
it gets changed to Vector v(5)
, and since the the original Vector
class declared in std_lib_facilities.h
requires a template argument, you get the error message posted in your question about a missing template argument.
To solve this you could either rename your class to something other than vector
, you could #undef vector
after you include std_lib_facilities.h
, or you could not include that header at all and just #include <iostream>
since the only part of the standard library you are using is std::cout
.
Upvotes: 1
Reputation: 10489
While searching online, I found your header file... which contains the line:
// disgusting macro hack to get a range checked vector:
#define vector Vector
I don't know why you are using it, but this is the cause of your problem.
To use a vector all you need to do is #include <vector>
To add some clarification, the header file creates a vector class called Vector
and then the #define
makes sure that all initialisations of vector
will actually refer to this Vector
class.
Renaming your class will get rid of this issue for you.
As a side note, that header file is horrid, be careful when using it.
Upvotes: 1