Reputation:
I been writing a program in which, an array will dynamically grow when ever the max size is reach. I have used std::unique_ptr, but I'm not sure what is causing the error. I'm using VS2010.
The error is:
error C2027: use of undefined type 'DyArray::Impl' c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory 2067
Here is the code
DyArray.h
#pragma once
#include <iostream>
#include <memory>
class DyArray
{
public:
DyArray(int IntialSize, int IncrementSize)
{
}
~DyArray()
{
}
void Insert(int Data);
void Set(int Position,int Data);
int Get(int Position);
void Print();
private:
DyArray(const DyArray&);
DyArray& operator=(const DyArray&);
struct Impl;
std::unique_ptr<Impl> m_Impl;
};
DyArray.cpp
#include "LinkedList.h"
#include <iostream>
struct DyArray::Impl
{
typedef struct
{
int* array;
size_t used;
size_t size;
}Array;
public:
Array* m_DyArray;
size_t m_InitalSize;
size_t m_IncrementSize;
Impl(Array* DyArray,int IntialSize,int IncrementSize):m_DyArray(DyArray),m_InitalSize(IntialSize),m_IncrementSize(IncrementSize)
{
m_DyArray->array = (int*)malloc(m_InitalSize * sizeof(int));
m_DyArray->used = 0;
m_DyArray->size = m_InitalSize;
}
~Impl()
{
free(m_DyArray->array);
m_DyArray->array = NULL;
m_DyArray->used = m_DyArray->size = 0;
}
void insertArray(int element)
{
if (m_DyArray->used == m_DyArray->size)
{
m_DyArray->size += m_IncrementSize;
m_DyArray->array = (int*)realloc(m_DyArray->array,m_DyArray->size*sizeof(int));
}
m_DyArray->array[m_DyArray->used++] = element;
}
void Display()
{
std::cout<<"\n";
for (int i = 0; i< m_DyArray->used;i++)
{
std::cout<<m_DyArray->array[i]<<" ";
}
}
};
void DyArray::Insert( int Data )
{
m_Impl->insertArray(Data);
}
void DyArray::Print()
{
m_Impl->Display();
}
Main.cpp
#include "DyArray.h"
#include <iostream>
void main()
{
DyArray dyarray(2,3);
dyarray.Insert(12);
dyarray.Insert(14);
dyarray.Insert(55);
dyarray.Insert(23);
dyarray.Insert(444);
dyarray.Insert(23);
dyarray.Print();
}
Upvotes: 0
Views: 157
Reputation: 254431
The destructor of your class needs to destroy the unique pointer, which in turn needs to delete the Impl
it manages. It can only do that if Impl
is a complete (i.e. fully defined) type.
Define the destructor in the source file, after the definition of Impl
, not in the class definition.
You'll also need to define the constructor there, if you want it to create an Impl
and initialise the pointer.
Upvotes: 1