gpt4 book ai didi

c++ - 具有继承性的未声明标识符(operator ostream)

转载 作者:太空宇宙 更新时间:2023-11-04 13:00:12 26 4
gpt4 key购买 nike

<分区>

我不明白为什么会出现错误,因为在我看来一切都很好,我已经加载了所有代码,但错误仅出现在打印运算符函数中。当我运行该程序时,他会带我进入此功能。

我有一个错误,但我不明白为什么。非常感谢帮助。

当我运行程序时,错误在这里(在 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;
}

主要:

#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;
}

猫笔.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;

猫笔.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;
}

谢谢...

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com