This question already has answers here:
Resolve build errors due to circular dependency amongst classes
                                
                                    (11个答案)
                                
                        
                                2年前关闭。
            
                    
我无法弄清楚为什么会出现错误,因为对我来说一切都很好,我已经加载了所有代码,但是错误仅在print operator的函数中。当我运行程序时,他带我去执行此功能。

我有一个错误,但我不明白为什么。
希望能有所帮助。

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


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


谢谢...

最佳答案

这个定义有错别字

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


缺少参数名称os。

 friend ostream& operator<<(ostream &os, const CatsPen& other){
                                     ^^^

10-07 12:29