c ++ - 具有继承的未声明标识符(运算符ostream)[重复]
Posted
技术标签:
【中文标题】c ++ - 具有继承的未声明标识符(运算符ostream)[重复]【英文标题】:c++ - undeclared identifier (operator ostream) with inheritance [duplicate] 【发布时间】:2017-06-10 14:21:25 【问题描述】:我不知道为什么会出现错误,因为对我来说一切都很好,我已经加载了所有代码,但错误出现在 print operator only 函数中。当我运行程序时,他会带我到这个函数。
我有一个错误,但我不明白为什么。 非常感谢帮助。
当我运行程序时,错误就在这里(在 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;
谢谢...
【问题讨论】:
通函包括?? @liran 你忘了在定义朋友ostream&运算符中指定参数名称os 请花一些时间到read about how to ask good questions,学习如何创建Minimal, Complete, and Verifiable Example 并向我们展示。operator<<()
的参数列表没有为第一个参数提供名称。
您无法理解“'os' : undeclared identifier”的哪一部分?
【参考方案1】:
这个定义有错别字
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)
^^^
【讨论】:
以上是关于c ++ - 具有继承的未声明标识符(运算符ostream)[重复]的主要内容,如果未能解决你的问题,请参考以下文章