Initialize static members in Template class
Feb 27, 2013 at 1:39pm UTC
I've been trying to initialize a static variable in my template class and I can't get it to work. The closest I've gotten is that the program compiles, outputs the correct information, but then immediately gives an error.
This is the relevant code:
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
#ifndef Array_h
#define Array_h
#include<iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
template <typename T>
class Array
{
public :
//Array(T const& value = T());
Array(int s = DEFAULTSIZE) // default constructor
{
cout << "dvalue is " << dvalue << endl;
size = s;
arr = new T [size];
if (!arr)
{
delete [] arr;
cerr << "error\n" ;
exit(EXIT_FAILURE);
}
for (int i = 0; i <= size; i++)
{
arr[i] = dvalue;
}
}
And main.cpp
1 2 3 4 5 6 7 8 9 10
template <typename T> T Array<T>:: dvalue =9;
main()
{
int size = 0;
string yes_no;
Array<int > a;
cout << a << endl;
}
And this is the output:
dvalue is 9
9999999999
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x0000000001174010 ***
a.out: malloc.c:2453: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Abort
Feb 27, 2013 at 1:48pm UTC
The static initialisation is fine.
The problem could be your for loop. I'm guessing you might want < size
rather than <= size
.
Feb 27, 2013 at 1:52pm UTC
I do not see the declaation of the static sata member dvalue. But in any case your program is invalid. For example instead of
for (int i = 0; i <= size; i++)
shall be
for (int i = 0; i < size; i++)
because you allocated size elements not size + 1 elements.
Also this code has no any sense
1 2 3 4 5 6
if (!arr)
{
delete [] arr;
cerr << "error\n" ;
exit(EXIT_FAILURE);
}
First of all an exception will be generated if the memory will not be allocated. And if arr will be equal to 0 when this statement delete [] arr; has no any sense.
Last edited on Feb 27, 2013 at 1:52pm UTC
Feb 27, 2013 at 3:01pm UTC
Thanks guys
I do not see the declaation of the static sata member dvalue.
What do you mean? Where does that belong?
I corrected the silly error in the for loop, and now the program is running correctly.
And I also am aware that the
if (!arr)
code needs work; I'll be working on that soon.
Feb 27, 2013 at 3:03pm UTC
@INeedAHero
What do you mean? Where does that belong?
All class members shall be declared inside the class definition.You are complaining that "I've been trying to initialize a static variable " and at the same time you did not show the declaration of sttatic data member dvalue.
Last edited on Feb 27, 2013 at 3:11pm UTC
Topic archived. No new replies allowed.