Structure const error under template formatting - c++

the fallowing code tells me that o.days and days from constructor can't be solved, has anyone an idea why?
template <class T> struct Array{
int days;
T * M;
};
contructor of the class:
void constr(Array<Expe> &o){
o=new Array;
o->days = days;
o->M = new Array[o->days];
}
EDIT (Luchian Grigore):
template <class T> struct Array{
int days;
T * M;
Array( int size ) : days(size), M(new int[size])
{
}
~Array()
{
delete[] M;
}
};
when i try to init an array in main like this:
int main(){
//Main function of the program. no pre/ post condition.
Array <Expe> A;
error:
enter code here..\M.cpp:18:15: error: no matching function for call to 'Array::Array()'

Array<Expe> &o is a reference to an Array<Expe> object, not a pointer. If you must re-initialize it, the syntax is.
o = Array<Expe>();
and you access the members via .:
o.days = days;
o.M = new Array[o.days];
EDIT:
I remember the same code from yesterday. Why are you agains using proper constructors?
template <class T> struct Array{
int days;
T * M;
Array( int size ) : days(size), M(new int[size])
{
}
~Array()
{
delete[] M;
}
};

Related

How to create our own dynamic array class in c++

I was trying yo create my own array class (similar to std::vector) just for fun but there is some problem...
The Array class code itself works and compiles successfully but throws an error if i try to instantiate an object of Array class.
#include<iostream>
template<typename type, int size>
class Array
{
private:
type _mArray[size] = new type[size];
public:
int Access(int index)
{
return _mArray[index];
}
int Len()
{
return size;
}
void Insert(int index, type val)
{
_mArray[index] = val;
}
~Array()
{
delete[] _mArray;
}
};//All code above compiles successfully
int main()
{
Array<int, 2> name; //this line throws an error
}
I am a bit new to C++ so if someone can explain then I will be very thankful....
Btw here is the error
Array initializer must be an initializer list
type _mArray[size] = new type[size];
The template instantiates with: type is int, and size is 2. Therefore, this becomes:
int _mArray[2] = new int[2];
This obviously does not make much sense. If you put this, verbatim, in your main() your C++ compiler will also serve you with the same complaint.
It's clear that the intent here is, simply:
type _mArray[size];
And nothing else.
P.S. Now, let's go back and reread what the suffering C++ compiler was struggling to communicate here:
Array initializer must be an initializer list
int _mArray[2] is, obviously, an array. There's an = stuck after it. Ok, this must be array initialization. How do you initialize an array in C++? With a braced initialization list, of course. This would be something like this, for example:
int _mArray[2]={1, 2};
The C++ compiler saw nothing of that kind, and was trying to tell you that.
#include<iostream>
template<typename type, int size>
class Array
{
private:
type * _mArray ;
public:
int Access(int index)
{
return _mArray[index];
}
int Len()
{
return size;
}
Array()
{
_mArray = new type[size];
}
~Array()
{
delete[] _mArray;
}
int& operator[](int index){
return _mArray[index];
}
};//All code above compiles successfully
int main()
{
Array<int, 2> name;
name[0] = 1024;
name[1] = 100;
for(int i= 0; i< name.Len(); i++)
{
std::cout<< name[i] << std::endl;
}
}
You can get it to build using the following minimal change:
## -4,7 +4,7 ## template<typename type, int size>
class Array
{
private:
- type _mArray[size] = new type[size];
+ type* _mArray;
public:
int Access(int index)
{
## -18,6 +18,10 ## class Array
{
_mArray[index] = val;
}
+ Array()
+ {
+ _mArray = new type[size];
+ }
~Array()
{
delete[] _mArray;
Basically, you should be initializing the array in your constructor, and store a pointer to it as a class member. The following code builds:
#include<iostream>
template<typename type, int size>
class Array
{
private:
type* _mArray;
public:
int Access(int index)
{
return _mArray[index];
}
int Len()
{
return size;
}
void Insert(int index, type val)
{
_mArray[index] = val;
}
Array()
{
_mArray = new type[size];
}
~Array()
{
delete[] _mArray;
}
};//All code above compiles successfully
int main()
{
Array<int, 2> name; //this line throws an error
}

C++ Initiating object with array in constructor

I am trying to initiate an object with an array. Is there a way to do it with pointers or should i find another way to do this.
EDIT: I want to write this code with dynamic memory allocation, I know vector is better way to solve this.
#include <iostream>
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t arr[]) {
sideCount = side;
valueOfSides = (t*)malloc(side * sizeof(t));
for (int counter; counter < side; counter++) {
valueOfSides[counter] = val[counter];
}
}
~die() {
free(valueOfSides);
}
};
int main() {
die<int> sixsided(6, {1,2,3,4,5,6});
}
The right ways to do this would be
std::vector<t> valueOfSides;
template<size_t len> die(t (&arr)[len])
: valueOfSides(std::begin(arr), std::end(arr))
{}
or
std::vector<t> valueOfSides;
die(std::initializer_list<t> arr) : valueOfSides(arr) {}
I think. Though really, the best answer is
std::vector<t> valueOfSides;
die(std::vector<t> arr) : valueOfSides(std::move(arr)) {}
One should never use raw pointers to own memory in C++, and virtually never use new or malloc. As it is, you have undefined behavior in your code because of misusing malloc.
If you're absolutely insane, or doing homework, it can be done with raw pointers, though I doubt I can get it entirely right without tests and a compiler.
template<class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t* arr) {
sideCount = 0;
std::size_t buffer_size = sizeof(t)*side;
char* buffer;
try {
buffer = new char[side];
valueOfSides = reinterpret_cast<t*>(buffer);
for(int i=0; i<side; i++) {
new(valueOfSides+i)t(arr[i]);
sideCount++;
}
} catch(...) {
for(int i=sideCount; i>=0; i--)
(valueOfSides+i)->~t();
delete[]buffer;
throw;
}
}
die& operator=(die&& rhs) {
sideCount = rhs.sideCount;
valueOfSides = rhs.valueOfSides;
rhs.valueOfSides = nullptr;
rhs.sideCount = 0;
return *this;
}
//die& operator=(const die& rhs) not shown because its super hard.
~die() {
for(int i=sideCount; i>=0; i--)
(valueOfSides+i)->~t();
delete[]reinterpret_cast<char*>(valueOfSides);
}
};
As we've said before, getting this stuff right is crazy hard. Use a std::vector.
Use std::vector.
#include <iostream>
#include <initalizer_list>
#include <vector>
template<class T>
class die {
public:
die() = default;
die(std::initializer_list<T> list)
: sides{list}
{ /* DO NOTHING */ }
private:
std::vector<T> sides{};
};
int main() {
die<int> sixsided({1,2,3,4,5,6});
}
One way you can do this, using more of a C technique, is a variable argument list:
#include <cstdarg>
#include <iostream>
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, ...) {
sideCount = side;
valueOfSides = new t[side];
va_list args;
va_start(args, side);
for (int counter = 0; counter < side; counter++) {
valueOfSides[counter] = va_arg(args, t);
}
va_end(args);
}
~die() {
delete[] valueOfSides;
}
};
int main() {
die<int> sixsided(6, 1,2,3,4,5,6);
}
Rather than passing an array, you're passing the parameters individually (i.e. no need for a temporary array) and using a va_list to access them.
Also, the calls to malloc and free were replaced with new and delete which is the C++ way of allocating and deallocating memory.
The C++ solution:
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t arr[]) {
sideCount = side;
valueOfSides = new T[side]
for (int counter = 0; counter < side; counter++) { //always initialize variables
valueOfSides[i] = arr[i];
}
}
~die() {
delete[] valueOfSides;
}
};
int main() {
int arr[6] = { 1,2,3,4,5,6 };
die<int> sixsided(6, arr);
}
The new operator is like malloc and the delete and delete[] operators are like free. They are dynamic allocators.
C solution:
template <class t>
class die {
private:
int sideCount;
t* valueOfSides;
public:
die(int side, t arr[]) {
sideCount = side;
valueOfSides = (t*)malloc(side * sizeof(t));
for (int counter = 0; counter < side; counter++) { //always initialize variables
valueOfSides[i] = arr[i];
}
}
~die() {
free(valueOfSides);
}
};
int main() {
int arr[6] = { 1,2,3,4,5,6 };
die<int> sixsided(6, arr);
}
Note: in C the <iostream> header will not work, this is C++ only.
There are other containers, namely std::vector, that can work, but this is the solution for your answer.

Unexpected Constructor called in class

There are some Classes: Array, NumericArray. Array is a template class, and NumericArray is a class inherited from Array designed to take int, double, etc.
Part of Header of NumericArray:
template <class T = int>
class NumericArray : public Array<T>{
private:
T* m_data;
int size;
public:
NumericArray<T> operator * (T factor)const;
};
here are constructors and some functions of NumericArray:
template <class T>
NumericArray<T>::NumericArray(){
m_data = new T[10];
size = 10;
}
template <class T>
NumericArray<T>::NumericArray(int n){
m_data = new T[n];
size = n;
}
template <class T>
NumericArray<T>::NumericArray(const NumericArray<T>& s_data){
m_data = new T[s_data.size];
// assign elements in the source array
for (int i = 0;i<=(s_data.Size()-1 ); i++){
m_data[i] = s_data.m_data[i];
}
size = s_data.Size();
}
/* Destructor */
template <class T>
NumericArray<T>::~NumericArray(){
delete [] m_data;
}
template <class T>
NumericArray<T> NumericArray<T>::operator * (T factor)const{
NumericArray<T> temp(size);
for (int i = 0; i<size;i++){
temp.m_data[i] = (*this)[i] *factor;
}
return temp;
}
And when I call it in the main(), something weird happens. For example:
NumericArray<int> intArray1(10);
NumericArray<int> intArray2;
for(int i =0; i<10;i++){
intArray1[i] = i;
intArray2[i] = i;
}
The 2 arrays do contain numbers 0-9, but if I call
NumericArray intArray4 = intArray1*2;
intArray4 consists of zero(0)s. It seems that the default constructor is called in the function and passed to Array4. And after the operator, Array1 and Array2 are still numbers 0-9
Below are the related code of Array
template class Array{
private:
T* m_data;
int size;
public:
Array(); // constructor
Array(int n); // constructor
Array(const Array<T>& s_data); //Copy Constructor
virtual ~Array(); // destructor
void SetElement(int i, const T& source);
T& GetElement(int i)const;
int Size() const;
int DefaultSize()const;
void DefaultSize(int n);
// Operator overloading
Array<T>& operator = (const Array<T>& source) ;
T& operator [](int i);
const T& operator [] (int i) const;
};
template <class T>
Array<T>::Array(){
// default constructor
m_data = new T[defaultSize]; // initialize T*
size = defaultSize; // initialize integer
}
template <class T>
Array<T>::Array(int n){
// Constructor with arguments
m_data = new T[n];
size = n;
}
template <class T>
Array<T>::Array(const Array<T>& s_data){
// Copy constructor
m_data = new T[s_data.Size()];
// assign elements in the source array
for (int i = 0;i<=(s_data.Size()-1 ); i++){
m_data[i] = T( s_data.m_data[i]);
}
size = s_data.Size();
defaultSize = s_data.Size();
}
template <class T>
T& Array<T>::operator [](int i) {
if (i > size|| i<0){
OutOfBoundsException a;
throw a;
}
return m_data[i];
}
Not sure if provided enough information. Any hint is greatly appreciated.
The easiest way to access base class data members when the base is a (dependent) class template, is a using declaration like this:
#include <iostream>
using namespace std;
template< class Item >
class Base
{
protected:
Item item_;
Base( Item const& v ): item_( v ) {}
};
template< class Item >
class Derived
: public Base< Item >
{
protected:
using Base<Item>::item_;
public:
auto value() const -> Item { return item_; }
Derived( Item const& v ): Base<Item>( v ) {}
};
auto main() -> int
{
Derived<int> const x( 42 );
cout << x.value() << endl;
}
Alternatively you can qualify the access, e.g. this->item_ or Base<Item>::item_.
That said, it’s usually not a good idea to let derived classes access base class data members directly.
Here is the problem: both NumericArray and Array have
T* m_data;
int size;
The function Array::operator[] is called from Array which uses Array::m_data, however the NumericArray::operator* sets NumericAray::m_data. It probably is working as you would expect, but you are reading from the wrong pointer.
Remove the members from NumericArray and make the members protected instead of private in Array. The second part is optional if the implementation is changed a little bit.

Template Specialization pointer to pointer and array passing template deduction

I have the following code I was writing to test template specialization. Is there a way to have the pointer decay to the type so that I can use template parameter deduction to get my M and N so I can have the rows and cols of the two dimensional array? I know I can vector of vector for a 2d array, but this is an exercise in template specialization. The following code works as is, but see if you uncomment the following and comment out the current T** constructor it doesn't work. As a result the constructor currently hard codes the number of rows to be used for deleting the data_ member, this is not ideal and ideally I would get this parameter from the template deduction. I was wondering if there is a way to make this work in the following code shown below.
//doesn't like this constructor
// template <unsigned int M, unsigned int N>
// Data ( T (&d)[M][N] ): data_(d), rows_(M), cols_(N) {};
#include <iostream>
template <typename T>
class Data
{
public:
Data ( const T& d ): data_(d) {};
Data ( T&& d ): data_(std::move(d)) {};
std::string getType() { return "Plain Data Type"; }
private:
T data_;
};
template <typename T>
class Data<T**>
{
public:
//doesn't like this constructor
// template <unsigned int M, unsigned int N>
// Data ( T (&d)[M][N] ): data_(d), rows_(M), cols_(N) {};
Data ( T** d ): data_(d), rows_(25) {};
~Data() {
for ( unsigned int i = 0; i < rows_; ++i)
{
delete [] data_[i];
}
delete [] data_;
}
std::string getType() { return "Pointer to Pointer Data Type"; }
private:
T** data_;
unsigned int rows_;
unsigned int cols_;
};
template <typename T>
class Data<T*>
{
public:
Data ( T* d ): data_(d) {};
~Data() { delete data_; }
std::string getType() { return "Pointer Data Type"; }
private:
T* data_;
};
int main ( int argc, char *argv[])
{
float f(9.65);
Data<int> d1(f);
std::cout << d1.getType() << std::endl;
int *i = new int(5);
Data<int*> d2(i);
std::cout << d2.getType() << std::endl;
int **j = new int*[25];
for ( int i = 0 ; i < 25; ++i)
j[i] = new int[50];
Data<int**> d3(j);
std::cout << d3.getType() << std::endl;
}
output:
Plain Data Type
Pointer Data Type
Pointer to Pointer Data Type
T** and T[n][m] are not equivalent (and I curse the collegiate professors that refuse to teach this). One is a pointer-to-pointer, the other is an array of T[m] of size n.
You can specialize your 2D array type like this:
template<typename T, size_t N, size_t M>
class Data<T[N][M]>
{
public:
Data(const T(&ar)[N][M])
{
for (size_t i=0;i<N;++i)
std::copy(std::begin(ar[i]), std::end(ar[i]), std::begin(data[i]));
}
std::string getType() { return "2D-Fixed Array Data Type"; }
private:
T data[N][M];
};
And one way to use it is like this:
float fdat[10][20];
Data<decltype(fdat)> d4(fdat);
std::cout << d4.getType() << std::endl;
Output
2D-Fixed Array Data Type

Type definition issue

i have a stucture:
template <class T> struct Array{
int days;
T * M;
Array( int size ) : days(size), M(new T[size])
{
}
~Array()
{
delete[] M;
}
};
void currentDay();
void add(int,int,Array &);
and a class:
class Expe {
private:
int hk; //HouseKeeping
int fo; //Food
int tr; //Transport
int cl; //Clothing
int tn; //TelNet
int ot; //Others
}
Class contructor is :
Expe::Expe() {
this->hk = hk;
this->fo = fo;
this->tr = tr;
this->cl = cl;
this->tn = tn;
this->ot = ot;
}
THE PROBLEM: in main function i can manipulate the structure with objects... using the setObj() function for example but when i try to define my functions in Controller or in Controller.h i get the fallowing error:
..\ListStruc.cpp:28:28: error: 'Array' is not a type
..\ListStruc.cpp: In function 'void add(int, int, int)':
..\ListStruc.cpp:31:4: error: request for member 'M' in 'A', which is of non-class type 'int'
EDIT:
void add(int cant, int tip,Array A){
//Adds to current day the amount to a specific type
A.M[currentDay]; // i try to use this object.
}
This declaration is incorrect:
void add(int,int,Array &);
Since Array is a class template, the add function needs to be a template as well:
template <class T>
void add(int,int,Array<T> &);
Additionally, your definition of the add function takes the parameter by value, while the declaration takes the parameter by reference.