标题:编译时奇怪的问题 貌似二义性了 但是在哪儿? 求解
只看楼主
at445
Rank: 1
等 级:新手上路
帖 子:19
专家分:4
注 册:2011-6-14
结帖率:66.67%
已结贴  问题点数:20 回复次数:3 
编译时奇怪的问题 貌似二义性了 但是在哪儿? 求解
#include <cstdlib>
#include <iostream>
using namespace std;
class Base
{
     private:
         int number;
         char name[10];
     public:
         Base(int a,char *n)
         {
                  number = a;
                  strcpy(name,n);
         }
         void show()
         {
              cout<<number<<'\t'<<name;     
         }                     
};

class Book:public Base
{
      private:
          char author[10];
      public:
          Book(int no,char *book, char *aut):Base(no,book)
          {
               strcpy(author,aut);
          }
          void show()
          {
               Base::show();
               cout<<'\t'<<author<<endl;
          }
};

class Reader:public Base
{
      private:
          int amount;
          Book books[10];
      public:
          Reader(int no,char *reader):Base(no,reader)
          {
               amount=0;           
          }
          void show(void)
          {
               cout<<"reader ";
               Base::show();   
               cout<<endl;
               cout<<"    borrow:"<<endl;
               for(int i=0; i<amount; i++)
               {
                    cout<<"         "<<i+1<<":";
                    books[i].show();
               }
          }
          void borrow(Book &b)
          {
               books[amount++]=b;
          }
};               
int main(int argc, char *argv[])
{
    Book book[]={Book(1,"hello","chen"),
              Book(2,"hi","chen"),
              Book(3,"fine","chen"),
              Book(4,"bye","chen")};
    Reader reader[]={  Reader(1,"zhang"),
                       Reader(2,"wang")};
    reader[0].borrow(book[1]);
    reader[1].borrow(book[4]);
    reader[0].show();
    reader[1].show();
    system("PAUSE");
    return EXIT_SUCCESS;
}
搜索更多相关主题的帖子: class void private include public 
2012-01-09 20:39
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
得分:7 
class Reader:public Base
{
      private:
          int amount;
          Book books[10]; // 你确定Book有默认的构造函数?
      public:
          Reader(int no,char *reader):Base(no,reader)
          {
               amount=0;           
          }
          void show(void)
          {
               cout<<"reader ";
               Base::show();   
               cout<<endl;
               cout<<"    borrow:"<<endl;
               for(int i=0; i<amount; i++)
               {
                    cout<<"         "<<i+1<<":";
                    books[i].show();
               }
          }
          void borrow(Book &b)
          {
               books[amount++]=b;
          }
};

My life is brilliant
2012-01-09 23:31
BianChengNan
Rank: 8Rank: 8
等 级:贵宾
威 望:13
帖 子:302
专家分:972
注 册:2011-11-30
得分:7 
以下是引用at445在2012-1-9 20:39:50的发言:

#include
#include
using namespace std;
class Base
{
     private:
         int number;
         char name[10];
     public:
         Base(int a,char *n)
         {
                  number = a;
                  strcpy(name,n);
         }
         void show()
         {
              cout<
哥们,我告诉你一个好方法啊,第一先看编译器产生的错误,能明白基本上就知道哪有问题了.
第二,利用二分法的思想,逐步排除你代码上的问题出在哪? 比如,你可以先注掉主函数中的代码片段,看看有没有问题.如果还有问题,代码问题不在这,否则代码问题出在这.....
定位好了问题再拿来问好不

我的群:149544757 C/C++/Assembly 喜欢交流的朋友进,进群请写消息
2012-01-10 10:41
我是菜鸟C
Rank: 4
等 级:业余侠客
帖 子:74
专家分:200
注 册:2011-3-27
得分:7 
          void borrow(Book &b)
          {
               books[amount++]=b;
          }
};            
感觉是这里出问题了。b不是Book的一个对象嘛 。。
要某你把=从载一下。
2012-01-10 11:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-359425-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.356367 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved