请大伙看看一个vector<基类> 成员访问派生类成员的问题?

yaoike 2008-10-20 04:55:21
请大伙帮忙看一下一个关于vector<基类>成员访问派生类成员以及输出的问题。

我有以下类层次:
class CAtom
{
public:
virtual ~CAtom( ) { }

virtual int EncodedLength( ) = 0;
virtual int Length( ) = 0;
virtual string toString( ) = 0;
};


class CAtomInt : public CAtom
{
public:
CAtomInt( );
virtual ~CAtomInt( );

int getValue( ) const;
void setValue( int iInt );

private:
int m_iInt;
};

class CAtomString : public CAtom
{
public:
CAtomString( );
virtual ~CAtomString( );

string getValue( ) const;
void setValue( string strString );

private:
string m_strString;
};


class CAtomList : public CAtom
{
public:
CAtomList( );
virtual ~CAtomList( );

vector<CAtom *> getValue( ) const;
vector<CAtom *> *getValuePtr( ) const;
void setValue( vector<CAtom *> vecList );
private:
vector<CAtom *> m_vecList;
};

CAtomList 类中的数据成员m_vecList存放的有可能是CAtomInt 类型的指针,也有可能是CAtomString 类型的指针,那我如何设计以上这些类,才可以正确输出m_vecList的内容。可以的话,请劳你写几句代码吧。我试过在基类CAtom写一个输出的虚函数,在CAtomInt, CAtomString,CAtomList也写上各自的实现,但好像不行。
代码如下:
CAtomList *cal1 = new CAtomList;
const char *pcFile = "a.torrent";
cal1 = (CAtomList *)DecodeFile(pcFile);
cout << cal1->isList() << endl;
vector<CAtom *>::iterator it;
for (it = cal1->getValuePtr()->begin(); it < cal1->getValuePtr()->end(); it++)
{
cout << ((CAtom *)(it))->getValue() << endl;
}


以上这段代码请问有什么错啊?这个问题困扰了我一天了,看了很多书也没啥主意,再大家帮帮我~~~ ,不甚感激!!!谢谢谢谢!!!



















...全文
278 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaoike 2008-10-21
  • 打赏
  • 举报
回复
各位可以回答一下我这个贴子的后面二个问题吗?非常感谢!
yaoike 2008-10-20
  • 打赏
  • 举报
回复
请见一下这里,各位。


http://topic.csdn.net/u/20081020/18/b9697ae4-feb0-4e67-8936-4850d038837c.html



在这个帖子里我自己没有好好整理一下所提的问题就提了,抱歉。
lann64 2008-10-20
  • 打赏
  • 举报
回复
没说清楚,是把输出放到派生类的虚函数定义中,在基类用一个非虚函数调用虚函数来实现输出。
lann64 2008-10-20
  • 打赏
  • 举报
回复
不要用这样的语句
cout < < cal1->isList() < < endl;
cout < < ((CAtom *)(it))->getValue() < < endl;

由于返回类型不一致,很难在基类里声明。把输出功能,放到派生类里去,用一个基类虚函数调用。这样就可以*it.foo()来实现输出了。
lann64 2008-10-20
  • 打赏
  • 举报
回复
是不是这个意思?
#include <iostream>
using namespace std;

class A
{
public:
void pout()
{
doout();
}
protected:
virtual void doout()=0;
};

class B : public A
{
public:
B(int t):i(t){}
private:
void doout()
{
cout<<i<<endl;
}
int i;
};

class C : public A
{
public:
C(string s):ss(s){}
private:
void doout()
{
cout<<ss<<endl;
}
string ss;
};


int main()
{
A * pb=new B(100);
A * pc=new C("abcde");

pb->pout();
pc->pout();

delete pb;
delete pc;

return 0;

}
yaoike 2008-10-20
  • 打赏
  • 举报
回复
to minorcxx :
已经实现了,只是我没有贴出来而已,谢谢说明,请问一下,我如何访问m_vecList的内容呢?我很困惑这一点?
yaoike 2008-10-20
  • 打赏
  • 举报
回复
to hai040:
我按照你所说的做了:
void CAtomString ::print(ostream &os)
{
os << m_strString;
}
ostream operator <<(ostream &os, CAtom* atom)
{
atom->print(os);
return os;
}

Linking...
btclient3.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class CAtom *)" (??6@YA?AV?$basic_ostream@DU?$char_traits@D@std@@@std@@A
AV01@PAVCAtom@@@Z) already defined in Bencode.obj
CAtom.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class CAtom *)" (??6@YA?AV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01
@PAVCAtom@@@Z) already defined in Bencode.obj
Debug/btclient3.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.
Creating browse info file...
它出现这些错误?
我查msdn,是说:
以不同的形式在不同库中的两个成员对象中定义了该符号,并且使用了这两个成员对象。
那是否是
ostream operator <<(ostream &os, CAtom* atom)
{
atom->print(os);
return os;
}
引起的,我现在把它放在 CAtom.cpp 文件中,是否把它放在别的地方?
minorcxx 2008-10-20
  • 打赏
  • 举报
回复
virtual int EncodedLength( ) = 0;
virtual int Length( ) = 0;
virtual string toString( ) = 0;
在派生类中要实现
yaoike 2008-10-20
  • 打赏
  • 举报
回复
to zmlovelx:
DecodeFile()函数在哪?

因为不是我想了解的问题,所以我刚才没有贴上来。

CAtom *Decode( const string &x, unsigned long iStart )
{
if( iStart < x.size( ) )
{
if( x[iStart] == 'i' )
return DecodeLong( x, iStart );
else if( isdigit( x[iStart] ) )
return DecodeString( x, iStart );
else if( x[iStart] == 'l' )
return DecodeList( x, iStart );
else if( x[iStart] == 'd' )
return DecodeDicti( x, iStart );

string temp = x.substr( iStart );

UTIL_LogPrint( "error decoding - found unexpected character %u, halting decode\n", (unsigned char)x[iStart] );
}
else
UTIL_LogPrint( "error decoding - out of range\n" );

return NULL;
}

CAtom *DecodeFile( const char *szFile )
{
FILE *pFile = NULL;

if( ( pFile = fopen( szFile, "rb" ) ) == NULL )
{
UTIL_LogPrint( "warning - unable to open %s for reading\n", szFile );

return NULL;
}

fseek( pFile, 0, SEEK_END );
unsigned long ulFileSize = ftell( pFile );
fseek( pFile, 0, SEEK_SET );
char *pData = (char *)malloc( sizeof( char ) * ulFileSize );
memset( pData, 0, sizeof( char ) * ulFileSize );
fread( (void *)pData, sizeof( char ), ulFileSize, pFile );
fclose( pFile );
string strFile( pData, ulFileSize );
free( pData );

return Decode( strFile );
}
yaoike 2008-10-20
  • 打赏
  • 举报
回复
to taodm :
你写了很多,很辛苦,可惜最重要的“怎么个不行”却不讲。
所以,你需要先去google并认真看看“提问的智慧”

哦,我知道了,我的确没有认真“看提问的智慧”。抱歉!但我是有上google去查询这个问题的。
还有,我想了解的是:如何访问m_vecList内的内容。



hai040 2008-10-20
  • 打赏
  • 举报
回复
什么虚函数?
在基类里写一个print,在子类里实现
再重载<<
void CAtomInt::print(ostream& os)
{
os << m_iInt;
}
ostream operator <<(ostream& os, const CAtom* atom)
{
atom.print(os);
return os;
}
帅得不敢出门 2008-10-20
  • 打赏
  • 举报
回复
DecodeFile()函数在哪?
taodm 2008-10-20
  • 打赏
  • 举报
回复
你写了很多,很辛苦,可惜最重要的“怎么个不行”却不讲。
所以,你需要先去google并认真看看“提问的智慧”

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧