1

I can not figure out why there is an error because everything looks fine to me, I've loaded all the code, but the error is in the print operator only function. When I run the program he takes me to this function.

I have an error, but I can not understand why. Would appreciate help.

When I run the program the error is here(in CatsPen.h):

Error   1   error C2065: 'os' : undeclared identifier   
c:\users\name\documents\visual studio 2013\projects\exe8\CatsPen.h  33  1   
EXE8

 friend ostream& operator<<(ostream&, const CatsPen& other){
    for (int i = 0; i < other.countStreet; i++){
        os << other.street[i] << endl;
    }
    for (int i = 0; i < other.countSiami; i++){
        os << other.siami[i] << endl;
    }
    return os;
    }

main:

#include <iostream>
#include "CatsPen.h"

using namespace std;


int main(){

CatsPen pen1;

int choice = -1;
while (choice == -1){
    cin >> choice;
    cout << "Enter your choice: " << endl;
    cout << "1-add street cat " << endl;
    cout << "2-add siami cat " << endl;
    cout << "3-print cats " << endl;
    cout << "4-print how many cats " << endl;
    cout << "5-exit" << endl;

    if (choice == 1){
        pen1.addStreet();
    }
}

system("pause");
return 0;
}

CatsPen.h:

include "Cat.h"
#include <iostream>
using namespace std;
#ifndef CatsPen_H
#define CatsPen_H

class CatsPen{
private:
StreetCat * street;
SiamiCat * siami;
int countStreet;
int countSiami;
int numOfCats;
int emptyCage;

public:
CatsPen();
~CatsPen();
int getCountCat(){
    return countStreet + countSiami;
}

bool Place();
bool addStreet();
bool addSiami();

friend ostream& operator<<(ostream&, const CatsPen& other){
    for (int i = 0; i < other.countStreet; i++){
        os << other.street[i] << endl;
    }
    for (int i = 0; i < other.countSiami; i++){
        os << other.siami[i] << endl;
    }
    return os;
}
};
#endif;

CatsPen.cpp:

catsPen.h"

CatsPen::CatsPen(){
this->street = NULL;
this->siami = NULL;
this->numOfCats = 0;
this->countStreet = 0;
this->countSiami = 0;
this->emptyCage = 5;
}

CatsPen::~CatsPen(){
for (int i = 0; i < countStreet; i++)
    delete &street[i];

delete[] street;

for (int i = 0; i < countStreet; i++)
    delete &street[i];

delete[]siami;
}

bool CatsPen::Place(){
if (emptyCage > 0){
    return true;
}

cout << "no have place in the pen" << endl;
return false;
}

bool CatsPen::addStreet(){
if (Place() == true){
    if (countStreet == 0){
        this->street = new StreetCat[1];
        cin >> this->street[countStreet];
        cout << this->street[countStreet];
    }
    else if (countStreet > 0){
        StreetCat* copyArray = new StreetCat[this->countStreet + 1];
        for (int i = 0; i < countStreet; i++){
            copyArray[i] = street[i];
            cin >> copyArray[countStreet];
            delete[] street;
            cout << copyArray[countStreet];
            street = copyArray;
        }
        countStreet++;
        emptyCage--;
    }

    cout << "no have place in the pen" << endl;
    return false;
}
}

bool CatsPen::addSiami() {//add siami cat to the pen
if (Place() == true) {
    if (countSiami == 0) {
        this->siami = new SiamiCat[1];
        cin >> this->siami[countSiami];
        cout << siami[countSiami];
    }
    else if (countSiami > 0) {
        SiamiCat*copyArray = new SiamiCat[this->countSiami + 1];
        for (int i = 0; i < countSiami; i++)
            copyArray[i] = siami[i];
        cin >> copyArray[countSiami];
        cout << copyArray[countSiami];
        delete[]siami;
        siami = copyArray;
    }
    countSiami++;
    emptyCage--;
    return true;
}

cout << "no have place in the pen" << endl;
return false;
}

thank's...

liran
  • 19
  • 3

1 Answers1

0

There is a typo in this definition

 friend ostream& operator<<(ostream&, const CatsPen& other){
                                   ^^^ 
    for (int i = 0; i < other.countStreet; i++){
        os << other.street[i] << endl;
    }
    for (int i = 0; i < other.countSiami; i++){
        os << other.siami[i] << endl;
    }
    return os;
    }

The parameter name os is missed.

 friend ostream& operator<<(ostream &os, const CatsPen& other){
                                     ^^^ 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335