LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 703|回复: 1

作一个结构清晰OO,链接有点问题。

[复制链接]
发表于 2006-7-3 19:38:41 | 显示全部楼层 |阅读模式

  1. #include <stdio.h>

  2. #define interface struct

  3. using namespace std;

  4. /*
  5. * Interface of this pure class, all methods were called by the pure class.
  6. */
  7. interface IXMLReaderContext            
  8. {
  9. public:
  10.       virtual void AddAttribute() const = 0;
  11.       virtual void Display() const = 0;
  12. };

  13. class TVReader : public IXMLReaderContext
  14. {
  15. public:
  16.       TVReader();
  17.       ~TVReader();

  18.       void AddAttribute() const;
  19.       void Display() const  { printf(" Inside TVReader...\n"); }
  20. }
  21. class HeadendFileReader : public IXMLReaderContext
  22. {
  23. public:
  24.       HeadendFileReader();
  25.       ~HeadendFileReader();

  26.       void AddAttribute() const;
  27.       void Display() const  { printf("Inside HeadendFileReader...\n"); }
  28. };
  29. class RegistryReader : public IXMLReaderContext
  30. {
  31. public:
  32.       RegistryReader();
  33.       ~RegistryReader();

  34.       void AddAttribute() const;
  35.       void Display() const  { printf("Inside RegistryReader...\n"); }
  36. };


  37. static void DisplayObject(IXMLReaderContext& Context);

  38. int main()
  39. {
  40.     TVReader                  tvreader;
  41.     HeadendFileReader  headendreader;
  42.     RegistryReader        registryreader;

  43.     DisplayObject(tvreader);
  44.     DisplayObject(headendreader);
  45.     DisplayObject(registryreader);

  46.     return 0;
  47. }

  48. static void DisplayObject(IXMLReaderContext& Context)
  49. {
  50.     Context.Display();
  51. }

复制代码

编译正常,链接有问题

  1. [root@root Parser]# g++ -c virtual.cpp -o virtual.o
  2. [root@root Parser]# g++ -o virtual virtual.o
  3. virtual.o(.text+0x19): In function `TVReader::TVReader()':
  4. : undefined reference to `vtable for TVReader'
  5. virtual.o(.text+0x39): In function `TVReader::TVReader()':
  6. : undefined reference to `vtable for TVReader'
  7. virtual.o(.text+0x48): In function `TVReader::~TVReader()':
  8. : undefined reference to `vtable for TVReader'
  9. virtual.o(.text+0x56): In function `TVReader::~TVReader()':
  10. : undefined reference to `vtable for TVReader'
  11. collect2: ld returned 1 exit status
  12. [root@root Parser]#
复制代码
发表于 2006-7-3 22:39:00 | 显示全部楼层
好像构造与解析函数, AddAttribute函数 都只有声明而没有实现?
  1. #include <stdio.h>
  2. #define interface struct
  3. using namespace std;
  4. /*
  5. * Interface of this pure class, all methods were called by the pure class.
  6. */
  7. interface IXMLReaderContext            
  8. {
  9. public:
  10.       IXMLReaderContext(){}
  11.       virtual ~IXMLReaderContext(){}
  12.       virtual void AddAttribute() const = 0;
  13.       virtual void Display() const = 0;
  14. };
  15. class TVReader : public IXMLReaderContext
  16. {
  17. public:
  18.       TVReader(){}
  19.       ~TVReader(){}
  20.       void AddAttribute() const{}
  21.       void Display() const  { printf(" Inside TVReader...\n"); }
  22. };
  23. class HeadendFileReader : public IXMLReaderContext
  24. {
  25. public:
  26.       HeadendFileReader(){}
  27.       ~HeadendFileReader(){}
  28.       void AddAttribute() const{}
  29.       void Display() const  { printf("Inside HeadendFileReader...\n"); }
  30. };
  31. class RegistryReader : public IXMLReaderContext
  32. {
  33. public:
  34.       RegistryReader(){}
  35.       ~RegistryReader(){}
  36.       void AddAttribute() const{}
  37.       void Display() const  { printf("Inside RegistryReader...\n"); }
  38. };
  39. static void DisplayObject(IXMLReaderContext& Context);
  40. int main()
  41. {
  42.     TVReader                  tvreader;
  43.     HeadendFileReader  headendreader;
  44.     RegistryReader        registryreader;
  45.     DisplayObject(tvreader);
  46.     DisplayObject(headendreader);
  47.     DisplayObject(registryreader);
  48.     return 0;
  49. }
  50. static void DisplayObject(IXMLReaderContext& Context)
  51. {
  52.     Context.Display();
  53. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表