Overload the stream insertion operator for nested structures/classes - c++

I want to overload the stream insertion operator for a structure nested within a class. How can I fix this error and make the function to work, or is there any alternative method to implement it?
struct S {
int a;
int b;
};
class T {
private:
S** arrayName;
int r;
int c;
public:
friend ostream& operator << (ostream& _os, const T& _t) {
for (int i = 0; i < _t.r; i++) {
for (int j = 0; j < _t.c; j++) {
_os << _t.arrayName[i][j];
}
}
return _os;
}
}

If arrayName was just an integer array within class T like:
int arrayName [4][4]; //for example
the current implementation would work. But as arrayName is a pointer to pointer to struct S and acts as a two-dimensional array of struct S, you will need to overload the << operator within struct S to be able to print its members a and b.
So your struct S should now be:
struct S {
int a;
int b;
friend ostream& operator<<(ostream& _os, const S& _s) {
_os << _s.a << ' ' << _s.b << endl;
return _os;
}
};

Related

ostream& does not name a type error. What am I doing wrong here?

I am a learner. I am working on operator overloading. I am trying to write a code for overloading [] and print the elements in the member array. But when I am overloading << to print the member array, I get the error, ostream& does not have a type. What am I doing wrong here? Also what can I do if I have a class that has two member arrays? Here is my code below:
#include <iostream>
#include <cassert>
class Digit
{
private:
int digit1[3]{0};
public:
int& operator[](const int index);
ostream& operator<<(ostream& out);
};
int& Digit::operator[](const int index)
{
return digit1[index];
}
ostream& Digit::operator<<(ostream& out)
{
int loop;
out << "{";
for (loop = 0; loop < 10; loop++)
{
out << digit1[loop] << " ";
}
out << "}";
return o;
}
int main()
{
using namespace std;
Digit n;
n[0] = 4;
n[1] = 3;
n[2] = 4;
n << cout;
return 0;
}
You have put
int main()
{
using namespace std;
//....
This cannot be seen where you declare your << operator. One solution is to change the signature to include the namesape:
In the class:
std::ostream& operator<<(std::ostream& out)
and then
std::ostream& Digit::operator<<(std::ostream& out)
While you are there, I wonder if it should be const?
you must be forgetting the namespace
try - std::ostream &operator<<(std::ostream &out)

C++ <<operator overloading with same type

I'm writing a method to print some spaces in std::cout, I know there are other ways using the standard library to achieve the same objective. Anyway, I used a typedef to store the number of spaces and an overloading of the << operator. But my overloading isn't called at all because my typedef is interpreted as an unsigned int.
So how can I tell to the compiler to call my function instead ?
class MyClass {
private:
typedef unsigned int space_type;
public:
std::ostream& operator<< (std::ostream& _os, space_type _n_spaces) {
for (int _C = 0; _C < _n_spaces; _C++)
_os << " ";
return _os;
}
void PrintNumbers(char _a, char _b) {
space_type spaces = 5;
std::cout << _a << spaces << _b << std::endl;
}
}
int main () {
MyClass class_instance;
class_instance.PrintNumbers('K', 'X');
std::cin.get();
return 0;
}
This is the expected output:
K X
This is the output I obtain:
K5X // 5 is interpreted as an unsigned int, so my overloaded function
// isn't called, instead is called the std overloading with unsigned int
Typedef doesn't create new type, it just creates alias of existing type. Possbile you could use something like this:
struct SpaceType {
int space_cnt;
};
...
std::ostream& operator<< (std::ostream& _os, SpaceType _n_spaces) {
for (int _C = 0; _C < _n_spaces.space_cnt; _C++)
_os << " ";
return _os;
}
...
SpaceType spaces = { 5 };
std::cout << _a << spaces << _b << std::endl;
Since you're defining space_type as an alias (i.e. typedef) and not a type, it is not distinguishable from int, and the compiler will issue an error if you attempted to overload operator(std::ostream&, int).
But what you're doing is defining a class member:
std::ostream& operator<< (std::ostream& _os, space_type _n_spaces)
When you define operators as class members, the first argument to the operator is (implicitly) an instance of the class. So in principle, that could only be called with:
MyClass m;
m << ???
But here is a problem: an operator function called using infix notation can only have two parameters, and in the case of a member operator function, the first argument is implicit. m << x can only be implemented by MyClass::operator<<(decltype(x)).
In short, you can only implement this with a non-member operator<< and the second argument to that overload must be a user type. So the following will work fine:
struct space_t {
unsigned x;
space_t(unsigned x) : x(x) {}
operator unsigned() const { return x; }
};
std::ostream& operator<< (std::ostream& os, space_t n) {
for (unsigned i = 0; i < n; ++i) os << " ";
return os;
}
See it on ideeone

When using templates, how do I fix the following compiler error to achieve the desired behavior of my program?

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

How to overload the << operator?

I am trying to build my own std::vector class but I am having trouble overloading the subscript ([]) and the cout << operators.
So far, I have tried to define the << operator as a public member of the class and then write the function's body outside the class declaration.
Right now I am getting terminal pages full of errors from iostream library and I do not even know where to begin looking through them.
I am a beginner in C++ and not accustomed to friend keyword or the template keyword. What am I doing wrong?
template<typename T>
class MyVector {
private:
T* Array;
public:
T& operator[](int b)
{
...
}
std::ostream& output(std::ostream& s) const;
};
std::ostream& operator<<(std::ostream& output, MyVector& A)
{
int i;
for(i = 0; i < A.GetDimension(); i++)
{
output << A[i] << " ";
}
output << "\n";
return output;
};
What you have is right, you just forgot the template declaration:
template <typename T>
std::ostream& operator<<(std::ostream& output, MyVector<T>& A)
{
int i;
for (i = 0; i < A.GetDimension(); i++)
{
output << A[i] << " ";
}
output << "\n";
return output;
};
template<typename T>
class MyVector{
...
std::ostream& printToStream(std::ostream& output) const {
int i;
for (i = 0; i < Dimension; i++) {
output << Array[i] << " ";
}
output << "\n";
return output;
}
...
};
template<typename T>
std::ostream& operator<<(std::ostream& output, const MyVector<T>& A) {
return A.printToStream(output);
}

Left of '.size' must have class/struct/union

Still cannot get this one to work. Please help!
template <typename T>
class Container{
public:
...
friend ostream& operator<<(ostream& ostr, const Container<T>& C)
{
for(size_t i=0; i!= data.size(); i++) // ERROR
ostr<<data[i]<<" ";
return ostr;
}
private:
vector<T> data;
};
data is a member of C and should therefore be accessed as C.data (remember that your operator<< is a free function and not a member of Container):
for(size_t i = 0; i != C.data.size(); ++i)
ostr << C.data[i] << " ";