首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于C++结构体链表有关问题

2013-08-14 
关于C++结构体链表问题上次有人说用ostream & operator ( ostream & os , DepTreeEntity temp )重载来

关于C++结构体链表问题
上次有人说用ostream & operator << ( ostream & os , DepTreeEntity temp );重载来实现对结构体通过链表的输出但是现在问题是我不止一个结构体。所以
struct DepTreeEntity;
struct VideoInChannelInfo;
struct ExDdcp;
ostream & operator << ( ostream & os , DepTreeEntity temp );
ostream & operator << ( ostream & os , VideoInChannelInfo temp );
ostream & operator << ( ostream & os , ExDdcp temp );
这样肯定是错的是吧,现在该怎么弄呢,先附上只有一个结构体,可以用重构实现结构体的输出,坐等大神帮忙。
struct DepTreeEntity
{
//成员变量
int  depId;
char depName[80];
//string depName;
int  parentId;
char fullPath[80];
int  nodeOrder;
int  onlineChannelCount;
int  offlineChannelCount;

//成员函数
void PrintId(void);
void PrintdepName(void);
void PrintparentId(void);
void PrintfullPath(void);
void PrintnodeOrder(void);
void PrintonlineChannelCount(void);
void PrintofflineChannelCount(void);

DepTreeEntity();
DepTreeEntity(int depId,const char* depName,int parentId,const char* fullPath,
int nodeOrder,int onlineChannelCount,int offlineChannelCount);
friend ostream & operator << ( ostream & os , DepTreeEntity temp );

};
DepTreeEntity stu1;
DepTreeEntity::DepTreeEntity()
{
depId = stu1.depId;
memcpy(depName,"TREE_depName",strlen("TREE_depName")+1);
parentId = stu1.parentId;
memcpy(fullPath,"TREE_fullPath",strlen("TREE_fullPath")+1);
nodeOrder = stu1.nodeOrder;
onlineChannelCount = stu1.onlineChannelCount;
offlineChannelCount = stu1.offlineChannelCount;
//memcpy(birthday,"stu_birthday",strlen("stu_birthday")+1);
}
DepTreeEntity::DepTreeEntity(int depId,const char* depName,int parentId, const char* fullPath, 
    int nodeOrder,int onlineChannelCount,int offlineChannelCount)
{
this->depId=depId;
memcpy(this->depName,depName,strlen(depName)+1);
this->parentId=parentId;
memcpy(this->fullPath,fullPath,strlen(fullPath)+1);
this->nodeOrder;
this->onlineChannelCount=onlineChannelCount;
this->offlineChannelCount=offlineChannelCount;
//memcpy(this->birthday,birthday,strlen(birthday)+1);
}
ostream & operator << ( ostream & os , DepTreeEntity temp )


{
os << "depId: " << temp.depId << endl 
   << "depName: " << temp.depName << endl
   << "parentId: " << temp.parentId << endl
   << "fullPath: "<< temp.fullPath << endl
   << "nodeOrder: "<< temp.nodeOrder << endl
   << "onlineChannelCount: "<< temp.onlineChannelCount << endl
   << "offlineChannelCount: "<< temp.offlineChannelCount << endl;
return os;
}
void DepTreeEntity::PrintId(void)
{
cout << depId << endl;
}
void DepTreeEntity::PrintdepName(void)
{
cout << depName << endl;
}
void DepTreeEntity::PrintparentId(void)
{
cout << parentId << endl;
}
void DepTreeEntity::PrintfullPath(void)
{
cout << fullPath << endl;
}
void DepTreeEntity::PrintnodeOrder(void)
{
cout << nodeOrder << endl;
}
void DepTreeEntity::PrintonlineChannelCount(void)
{
cout << onlineChannelCount << endl;
}
void DepTreeEntity::PrintofflineChannelCount(void)
{
cout << offlineChannelCount << endl;
}
以上所写就是只有一个struct DepTreeEntity结构体时候,实现函数Getentity(list<DepTreeEntity>*L )的实现方式,当有三个结构体时候,重载<<不行该怎么办?
[解决办法]


struct DepTreeEntity;
struct VideoInChannelInfo;
struct ExDdcp;
ostream & operator << ( ostream & os , const DepTreeEntity &temp );
ostream & operator << ( ostream & os , const VideoInChannelInfo &temp );


ostream & operator << ( ostream & os , const ExDdcp &temp );

三个不同的结构体链表,
这样是可以的,c++中 支持函数重载的。传入参数不一样,
会自动匹配调用对应接口的

热点排行