So what I'm trying to do is write a class with a template class as its data member. This is part of my homework.
So far this is what I have:
letter_frequencies.h
#include "ltr_freq_pair.h"
#include "darray.h"
class letter_frequencies
{
private:
darray<letterFrequencyStruct> array;
public:
letter_frequencies();
void outputFrequencies();
};
letter_frequencies.cpp
#include <iostream>
#include "letter_frequencies.h"
letter_frequencies::letter_frequencies()
{}
void letter_frequencies::outputFrequencies()
{
for(int index = 0; index < 26; index ++)
{
std::cout << "\n" << array[index].letter;
std::cout << " " << array[index].frequency;
}
}
Everything compiles, but once I run the program it has this error:
a.out: darray.cpp:44: T& darray<T>::operator[](unsigned int) [with T = letterFrequencyStruct]: Assertion 'index < used' failed.
Abort (core dumped)
The code snippet I'm trying to use for this class is:
//Check if Class Works
letter_frequencies array;
array.outputFrequencies();
Right now, the darray works just fine and the program does exactly what I need it to do, but one of the requirements is to have a class named letter_frequency with a data member darray<structTypename> name, which is why I'm fussing with this in the first place.
Also, how would I make the class call the explicit constructor for the darray? I want to set the size to 26.
darray.h
#ifndef DARRAY_H
#define DARRAY_H
#include <iostream>
template <typename T>
class darray
{
public:
//Constructors
darray();
darray(const darray&);
explicit darray(unsigned);
//Destructor
~darray();
//Constant Members
unsigned size() const {return used;}
unsigned capacity() const {return cap;}
//Modification Members
T& operator [] (unsigned);
void pop_back();
void push_back(const T&);
const T* find(const T&) const;
void resize(unsigned);
//Operators
T& operator = (const darray&);
bool operator == (const darray&) const;
//Friends
template <typename X>
friend std::ostream& operator << (std::ostream& output, const darray<X>& array);
private:
T* arr;
unsigned used, cap;
};
template <typename X>
std::ostream& operator << (std::ostream& output, const darray<X>& array)
{
for(int index = 0; index < array.size; index++)
{
output << index << array[index];
}
return output;
}
#include "darray.cpp"
#endif
As I mentioned before, you'd have to make letter_frequencies constructor like this:
letter_frequencies::letter_frequencies() : array(26) {
}
(so you initialize the array with the necessary number of elements).
BUT: I don't think you should make your own home-grown array when std::vector would be better...
Related
I've searched throughout my entire code, and there aren't any other issues except for this section. The semicolon at the end of the forward-slashed line is the issue. Is there something I'm missing? Full error message below.
source\repos\Square Matrix\SquareMatrix.cpp(108,27): error C2760: syntax error: unexpected token ';', expected '{'
#pragma once
#include <stdexcept>
#include "SquareMatrix.h"
template<typename T>
class SquareMatrix
{
private:
size_t _size; //will be the size of the array. size_t makes sure i only use nonnegative integers for the size
T **_ptr;
public:
SquareMatrix(); //zero parameter constructor
SquareMatrix(const SquareMatrix& rhs); //copy constructor
~SquareMatrix(); //destructor
SquareMatrix(SquareMatrix&& rhs); //Move Constructor
SquareMatrix& operator=(const SquareMatrix& rhs); //Copy assignment Operator
SquareMatrix& operator=(SquareMatrix&& rhs); //Move Assignment Operator
void resize(size_t new_size);
T& at(int row, int column);
size_t size() const;
};
template<typename T>
void SquareMatrix<T>::resize(size_t new_size)
{
std::cout << "Input size of the new matrix: " << std::endl;
std::cin >> new_size;
for (int i = 0; i < _size; i++)
{
delete[] _ptr[i];
}
if (_ptr != nullptr)
delete[] _ptr;
if (new_size != _size)
{
_ptr = new T * [new_size];
for (int i = 0; i < new_size; i++)
{
// _ptr[i] = T *[new_size]; //
}
}
}
It seems that the issue might be in the declaration of your class SquareMatrix. You have not posted it so I cant tell for sure but I tested your method implementation with a dummy class and I don't get any errors in compilation.
Make sure that your class declaration and method declaration have this syntax:
template<class T>
class SquareMatrix {
T a, b;
public:
void resize(size_t new_size);
};
You can review the section on Class Templates to get some examples and a more through explanation.
http://www.cplusplus.com/doc/oldtutorial/templates/
Hi I'm still wondering why I'm getting this error message :
Use of class template 'Array' requires template arguments
Header :
#ifndef Array_h
#define Array_h
template< typename T>
class Array
{
public:
Array(int = 5);
Array( const Array &);
const Array &operator=(Array &);
T &operator[](int);
T operator[](int) const;
// getters and setters
int getSize() const;
void setSize(int);
~Array();
private:
int size;
T *ptr;
bool checkRange(int);
};
#endif
CPP file
template< typename T >
const Array &Array< T >::operator=(Array &other)
{
if( &other != this)
{
if( other.getSize != this->size)
{
delete [] ptr;
size = other.getSize;
ptr = new T[size];
}
for ( int i = 0; i < size; i++)
{
ptr[i] = other[i];
}
}
return *this;
}
Problem seems to do with returning a const reference to object.
Thanks.
Before the compiler sees Array<T>::, it doesn't know that you are defining a member of the class template, and therefore you cannot use the injected-class-name Array as shorthand for Array<T>. You'll need to write const Array<T> &.
And you got constness backwards in your assignment operator. It should take a const reference and return a non-const one.
Also, Why can templates only be implemented in the header file?
I'm practicing templates in Visual Studio 2013 from a not so well written C++ book and I receive the following compiler errors respectively, "error C4430: missing type specifier - int assumed. Note: C++ does not support default-int" and "error C2143: syntax error : missing ',' before '<'".
Both refer me to line 10 which is...
std::ostream& operator<< (std::ostream&, const Array<T>&);
The desired behavior of my code is to show the creation and destruction of temporary Animal objects using templates.
The troubleshooting steps I have tried so far include substituting std::iostream& with int in my return type and first parameter type of line 10. After doing so the error messages remained the same. This seemed to suggest that the problem may be the second parameter type. I then added the keyword typename to the second parameter of my overloading operator function (operator<<) on line 10 and the error still persisted.
Line 10 can be found in the header file below.
//Array.h
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include "Animal.h"
const int DefaultSize = 3;
template <typename T>
std::ostream& operator<< (std::ostream&, const Array<T>&);
template <typename T> // declare the template and the paramenter
class Array // the class being parameterized
{
public:
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete[] pType; }
// operators
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const { return pType[offSet]; }
// accessors
int GetSize() const { return itsSize; }
// friend function
friend std::ostream& operator<< (std::ostream&, const Array<T>&);
private:
T *pType;
int itsSize;
};
template <typename T>
Array<T>::Array(int size = DefaultSize) :itsSize(size)
{
pType = new T[size];
for (int i = 0; i < size; i++)
pType[i] = static_cast<T>(0);
}
Array<Animal>::Array(int AnimalArraySize) :itsSize(AnimalArraySize)
{
pType = new Animal[AnimalArraySize];
}
template <typename T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.GetSzie();
pType = new T[itsSize];
for (int i = 0; i < itsSize; i++)
pType[i] = rhs[i];
}
template <typename T>
Array<T>& Array<T>::operator=(const Array &rhs)
{
if (this == &rhs)
return *this;
delete[] pType;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i < itsSize; i++)
pType[i] = rhs[i];
return *this;
}
template <typename T>
std::ostream& operator<< (std::ostream& output, const Array<T> &theArray)
{
for (int i = 0; i < theArray.GetSize(); i++)
output << "[" << i << "]" << theArray[i] << std::endl;
return output;
}
#endif
Here's the rest of my code from my book for your reference.
//Animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
#include <iostream>
class Animal
{
public:
// constructors
Animal();
Animal(int);
~Animal();
// accessors
int GetWeight() const { return itsWeight; }
void SetWeight(int theWeight) { itsWeight = theWeight; }
// friend operators
friend std::ostream& operator<<(std::ostream&, const Animal&);
private:
int itsWeight;
};
#endif
//Animal.cpp
#include "Animal.h"
#include <iostream>
Animal::Animal() :itsWeight(0)
{
std::cout << "animal() ";
}
Animal::Animal(int weight) : itsWeight(weight)
{
std::cout << "animal(int) ";
}
Animal::~Animal()
{
std::cout << "Destroyed an animal...";
}
//Main.cpp
#include <iostream>
#include "Animal.h"
#include "Array.h"
std::ostream& operator<<(std::ostream&, const Animal&);
void IntFillFunction(Array<int>& theArray);
void AnimalFillFunction(Array<Animal>& theArray);
int main()
{
Array<int> intArray;
Array<Animal> animalArray;
IntFillFunction(intArray);
AnimalFillFunction(animalArray);
std::cout << "intArray...\n" << intArray;
std::cout << "\nanimalArray...\n" << animalArray << std::endl;
std::cin.get();
return 0;
}
std::ostream& operator<<(std::ostream& theStream, const Animal& theAnimal)
{
theStream << theAnimal.GetWeight();
return theStream;
}
void IntFillFunction(Array<int>& theArray)
{
bool Stop = false;
int offset, value;
while (!Stop)
{
std::cout << "Enter an offset (0-9) and a value. ";
std::cout << "(-1 to stop): ";
std::cin >> offset >> value;
if (offset < 0)
break;
if (offset > 9)
{
std::cout << "***Please use values between 0 and 9.***\n";
continue;
}
theArray[offset] = value;
}
}
void AnimalFillFunction(Array<Animal>& theArray)
{
Animal *pAnimal;
for (int i = 0; i < theArray.GetSize(); i++)
{
pAnimal = new Animal(i * 10);
theArray[i] = *pAnimal;
delete pAnimal;
}
}
I've tried my best to keep this question on topic by providing you with the desired behavior of the program, a specific problem or error and the shortest code necessary to reproduce it. All of which are requirements for debugging help as documented in the Help Center page.
I'm new to this website, and if my question is still off-topic, please let me know how I can change my question to be more on topic or inform me of a better place to ask my question. - Thank you
This function is declared before the Array class is declared (i.e., the compiler has never seen that type and doesn't know what it is, hence your error).
template<typename> class Array; // Add this forward declaration
template <typename T>
std::ostream& operator<< (std::ostream&, const Array<T>&);
Edit
I just noticed that the operator<< is declared before the Array class and as a friend function within the Array class. You could just remove the declaration at the top.
Edit 2
Array.h
I removed the following items
#include "Animal.h"
template <typename T>
std::ostream& operator<< (std::ostream&, const Array<T>&);
Array<Animal>::Array(int AnimalArraySize) :itsSize(AnimalArraySize)
{
pType = new Animal[AnimalArraySize];
}
Updated the friend function declartion to be
template<typename T> // This is not a member function so must be declared as a template
friend std::ostream& operator<< (std::ostream&, const Array<T>&);
Animal.h
This class has the friend function but it's not implemented in the cpp file. I moved the definition from the main.cpp into Animal.cpp
I'm trying to test the following exception handling code:
#include "Array_Template.h"
using namespace std;
int main()
{
Array<int> intArr1;
//attempt to use out-of-range subscript
try{
cout << "\nAttempt to assign 1000 to intArr1[6]" << endl;
intArr1[6] = 1000;
} //end try
catch (const out_of_range &ex){
cout << "An exception occurred: " << ex.what() << endl;
} //end catch
return 0;
} //end main
In my Array class template:
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <stdexcept>
//forward declarations of friend functions as specializations of template
template<typename T> class Array;
template<typename T>
std::istream &operator>>(std::istream &, Array<T> &);
template<typename T>
std::ostream &operator<<(std::ostream &, const Array<T> &);
//Array class template
template<typename T>
class Array
{
friend std::istream &operator>> <>(std::istream &, Array<T> &);
friend std::ostream &operator<< <>(std::ostream &, const Array<T> &);
public:
Array(int = 5); //default constructor of array size 5
Array(const Array<T> &); //copy constructor
~Array(); //destructor
int getSize() const; //return size of array
const Array<T> &operator=(const Array<T> &); //overloaded assignment operator
bool operator==(const Array<T> &) const; //overloaded equality operator
bool operator!=(const Array<T> &r) const //overloaded inequality operator, inline definition
{
return !(*this == r); //invokes Array<T>::operator==
}
T &operator[](int); //overloaded subscript operator for non-const objects
T operator[](int) const; //overloaded subscript for const objects
private:
int size; //pointer-based array size
T *arrPtr; //pointer to first element of array
}; //end class Array
template<typename T>
Array<T>::Array(int s)
:size(s > 0 ? s : 5), arrPtr(new T[size])
{
for (int i = 0; i < size; ++i)
arrPtr[i] = 0;
} //end Array constructor
template<typename T>
//ref return creates a modifiable lvalue
T& Array<T>::operator[](int index)
{
if (index < 0 || index >= size) //check if out of bounds
throw std::out_of_range::out_of_range("index out of range");
return arrPtr[index]; //ref return
} //end operator[]
template<typename T>
//const ref return creates an rvalue
T Array<T>::operator[](int index) const
{
if (index < 0 || index >= size) //check if out of bounds
throw std::out_of_range::out_of_range("index out of range");
return arrPtr[index]; //returns copy of this element
} //end operator[]
#endif
The program compiles and executes fine in Visual Studio, displaying the correct one-line error message:
But when when I compile the same program with gcc and runs it, I get the following output:
* Error in `./arrays_template': free(): invalid size: 0x0000000001426030 *
======= Backtrace: ========= /lib64/libc.so.6(+0x7364f)[0x7fd7c2e3d64f]
/lib64/libc.so.6(+0x78eae)[0x7fd7c2e42eae]
/lib64/libc.so.6(+0x79b87)[0x7fd7c2e43b87] ./arrays_template[0x4011a1]
./arrays_template[0x400fc7]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fd7c2debbe5]
./arrays_template[0x400c29]
which is followed by a lengthy memory map trace.
Could someone explain what exactly has occurred here and how to fix it? By fixing I mean how I could get the same error message to be printed as specified in the catch block.
Below code is wrong:
throw std::out_of_range::out_of_range("index out of range");
// ^^^^^^^^^^^^^^
It should be re-written as:
throw std::out_of_range("index out of range");
To call the constructor of std::out_of_range
Also make sure to revise the return type of operator[](int) const.
Change this statement in the both operators from
throw std::out_of_range::out_of_range("index out of range");
to
throw std::out_of_range("index out of range");
just wrote a code for a template array class (I know its not finished yet), and trying to remember how to overload operators (meanwhile without special difficulties...).
Anyway, while thinking of how to implement operator[] I wondered what would happen if the index is outside the boundaries of the array... I'm pretty sure it is not possible for me to return a NULL (because of the return type), right? and if so, what should I return in case the index is out of boundries?
here's the code, most of it is redundant to my question, but it might help anyone who google's operators overloading so I post the complete code...
#ifndef __MYARRAY_H
#define __MYARRAY_H
#include <iostream>
using namespace std;
template <class T>
class MyArray
{
int phisicalSize, logicalSize;
char printDelimiter;
T* arr;
public:
MyArray(int size=10, char printDelimiter=' ');
MyArray(const MyArray& other);
~MyArray() {delete []arr;}
const MyArray& operator=(const MyArray& other);
const MyArray& operator+=(const T& newVal);
T& operator[](int index);
friend ostream& operator<<(ostream& os, const MyArray& ma)
{
for(int i=0; i<ma.logicalSize; i++)
os << ma.arr[i] << ma.printDelimiter;
return os;
}
};
template <class T>
T& MyArray<T>::operator[]( int index )
{
if (index < 0 || index > logicalSize)
{
//do what???
}
return arr[index];
}
template <class T>
const MyArray<T>& MyArray<T>::operator+=( const T& newVal )
{
if (logicalSize < phisicalSize)
{
arr[logicalSize] = newVal;
logicalSize++;
}
return *this;
}
template <class T>
const MyArray<T>& MyArray<T>::operator=( const MyArray<T>& other )
{
if (this != &other)
{
delete []arr;
phisicalSize = other.phisicalSize;
logicalSize = other.logicalSize;
printDelimiter = other.printDelimiter;
arr = new T[phisicalSize];
for(int i=0; i<logicalSize; i++)
arr[i] = other.arr[i];
}
return *this;
}
template <class T>
MyArray<T>::MyArray( const MyArray& other ) : arr(NULL)
{
*this = other;
}
template <class T>
MyArray<T>::MyArray( int size, char printDelimiter ) : phisicalSize(size), logicalSize(0), printDelimiter(printDelimiter)
{
arr = new T[phisicalSize];
}
#endif
operator[] generally does no bounds checking. Most of the standard containers that can have a range utilize a separate function, at(), which is range checked and throws a std::out_of_range exception.
You likely want to implement a const T& operator[] overload, too.