注册 登录
编程论坛 C++教室

急求 多继承派生程序错误!急!急

zhonglai123 发布于 2008-08-13 17:30, 608 次点击
哪位大虾看一下我的这个程序
是关于派生类的多继承的 这个程序要是不要带参数可以运行通过
但是加上参数后就显示下面的错误 仔细查了没有错误啊为什呢
这个程序是我从教材上的抄下来的
#include<iostream.h>
Class base1
{
 Public:
    base1(int i)
    {
      cout<<"constrcting base1"<<endl;
          x1=i;
        }
    void print()
    {cout<<"x1= "<<x1<<endl;
    }
 protected:
    int x1;
};
class base2
{
public:    
    base2(int j)
    {cout <<"constructing base2"<<endl;
     x2=j;
    }
    void print()
    {cout<<"x2="<<x2<<endl;
    }
    proteced:
    int x2;
};
class base3
{
public:
    base3()
    {cout<<"constructing class base3"<<endl;
    }
    void print()
    {cout<<"constructing class base3 no values"<<endl;
    }
};
class M : public base1,public base2, public base3
    {
     public:
         M(int a,int b,int c,int d):base1(a),A(c),base2(b),B(d){}
     void print()
     { base1::print();
       base2::print();
       base3::print();
     }
    };
void main()
{ M obj(1,2,3,4);
 obj.print();
}

显示的错误是:
error c2629: unexpected 'class M ('
error c2334: unexpected token(s) proceding ':';skipping apparent function body
error c2660: M::M :function does not take 4 paraments

[[it] 本帖最后由 zhonglai123 于 2008-8-13 17:32 编辑 [/it]]
3 回复
#2
zhonglai1232008-08-13 17:31
回复 1# zhonglai123 的帖子
请大侠们给我个回复吧
急死我了
#3
zerocn2008-08-13 18:21
不知道你是再这里打的还是怎么样,很多错误

以下的编译通过,错误的地方请对应查找
#include <iostream>
#include <stdlib.h>
using namespace std;

class base1//class 的大小写
{
public://还是大小写
    base1(int i)
    {
      cout<<"constrcting base1"<<endl;
          x1=i;
        }
    void print()
    {cout<<"x1= "<<x1<<endl;
    }
protected:
    int x1;
};

class base2
{
public:   
    base2(int j)
    {cout <<"constructing base2"<<endl;
     x2=j;
    }
    void print()
    {cout<<"x2="<<x2<<endl;
    }
    protected://打错!!protected:
    int x2;
};

class base3
{
public:
    base3()
    {cout<<"constructing class base3"<<endl;
    }
    void print()
    {cout<<"constructing class base3 no values"<<endl;
    }
};

class M : public base1,public base2, public base3
{
     public:
     M(int a,int b,int c,int d):base1(a),base2(b){}//没有A、B成员!
     void print()
     { base1::print();
       base2::print();
       base3::print();
     }
};
int main()
{ M obj(1,2,3,4);
  obj.print();
  system("pause");
  return 0;
}

[[it] 本帖最后由 zerocn 于 2008-8-13 18:22 编辑 [/it]]
#4
elegant872008-08-13 21:24
#include<iostream>

using namespace std;

class base1
{
public:
    base1(int i)
    {
          cout<<"constrcting base1"<<endl;
          x1=i;
    }
    void print()
    {
         cout<<"x1= "<<x1<<endl;
    }
protected:
    int x1;
};
class base2
{
public:   
    base2(int j)
    {
         cout <<"constructing base2"<<endl;
         x2=j;
    }
    void print()
    {
         cout<<"x2="<<x2<<endl;
    }
protected:
    int x2;
};

class base3
{
public:
    base3()
    {
         cout<<"constructing class base3"<<endl;
    }
    void print()
    {
         cout<<"constructing class base3 no values"<<endl;
    }
};
class M : public base1,public base2, public base3
{
public:
     M(int a,int b,int c,int d):base1(a),base2(b),A(c),B(d){}
     void print()
     {
           base1::print();
           base2::print();
           base3::print();
     }
protected:
     int A;
     int B;
};

int main()
{
     M obj(1,2,3,4);
     obj.print();
     system("pause");
     return 0;
}
1