problem with multiple inheritance

Discussion in 'C++' started by Tony Johansson, Aug 16, 2005.

  1. Hello experts!

    I have a small program that is using multiple inheritance.
    There are 4 classes involved
    I get 4 compile error that I can't figure out why.
    It's this row which is located in the main program see below that is causing
    these compile errors.
    cout << p->getName() << endl;
    Here is the first compile error the other might be a consequesce of the
    first one.
    c:\Documents and Settings\Tony\kau\cplusplus\test4\start.cpp(13): error
    C2446: '<' : no conversion from 'std::vector<_Ty>::size_type (__thiscall
    std::vector<_Ty>::* )(void) const' to 'int'
    with
    [
    _Ty=Person *
    ]

    Here are all the class definitions
    *********************
    #include <string>
    using namespace std;
    class Person
    {
    public:
    Person(string nn = "default") : name(nn) {}
    string getName() const
    {
    return name;
    }
    private:
    string name;
    };

    class Student : public virtual Person
    {
    public:
    Student(string nn="default") : Person(nn) {}
    };

    class Employee : public virtual Person
    {
    public:
    Employee(string nn="default") : Person(nn) {}
    };

    class TeachingAssistent : public Student, public Employee
    {
    public:
    TeachingAssistent(string nn="default") : Person(nn) {}
    };

    Here is main program
    ****************
    #include <vector>
    #include "person.h"
    #include <iostream>
    using namespace std;

    int main()
    {
    vector<Person *> p;
    p.push_back(new Student);
    p.push_back(new Employee);
    p.push_back(new TeachingAssistent);

    for(int i=0; i < p.size; i++)
    cout << p->getName() << endl;

    return 0;
    }

    Many thanks

    //Tony
     
    Tony Johansson, Aug 16, 2005
    #1
    1. Advertisements

  2. Tony Johansson wrote:
    > [...]
    > for(int i=0; i < p.size; i++)


    If you intend to find the size of the vector, you need to _call_ the
    function 'size'. For that you need to supply the parentheses:

    ... i < p.size() ...

    > cout << p->getName() << endl;
    >
    > return 0;
    > }


    V
     
    Victor Bazarov, Aug 16, 2005
    #2
    1. Advertisements

  3. Le mardi 16 août 2005 à 21:16:49, Tony Johansson a écrit dans
    comp.lang.c++ :

    > Hello experts!
    >
    > I have a small program that is using multiple inheritance.
    > There are 4 classes involved
    > I get 4 compile error that I can't figure out why.
    > It's this row which is located in the main program see below that is causing
    > these compile errors.
    > cout << p->getName() << endl;


    Actually, no, it's *this* line:

    for(int i=0; i < p.size; i++)

    > Here is the first compile error the other might be a consequesce of the
    > first one.
    > c:\Documents and Settings\Tony\kau\cplusplus\test4\start.cpp(13): error
    > C2446: '<' : no conversion from 'std::vector<_Ty>::size_type (__thiscall
    > std::vector<_Ty>::* )(void) const' to 'int'
    > with
    > [
    > _Ty=Person *
    > ]


    You have a problem with the comparison: (error C2446: '<') because
    you're comparing an int (i) with a function pointer (p.size). 'size' is
    not a piece of data, but a function; you want this:

    for (int i=0; i < p.size(); i++)

    >
    > for(int i=0; i < p.size; i++)
    > cout << p->getName() << endl;



    --
    ___________ 16/08/2005 21:29:42
    _/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
    \ \_L_) Il faut donc que les hommes commencent
    -'(__) par n'être pas fanatiques pour mériter
    _/___(_) la tolérance. -- Voltaire, 1763
     
    Serge Paccalin, Aug 16, 2005
    #3
  4. Hi,

    your problem is not related to multiple inheritance at all. The problem is
    that you forgot the brackets behind the function call p.size() in the for
    loop.

    Greetings,
    Ruwen

    "Tony Johansson" <> schrieb im Newsbeitrag
    news:BYqMe.31661$...
    > Hello experts!
    >
    > I have a small program that is using multiple inheritance.
    > There are 4 classes involved
    > I get 4 compile error that I can't figure out why.
    > It's this row which is located in the main program see below that is
    > causing these compile errors.
    > cout << p->getName() << endl;
    > Here is the first compile error the other might be a consequesce of the
    > first one.
    > c:\Documents and Settings\Tony\kau\cplusplus\test4\start.cpp(13): error
    > C2446: '<' : no conversion from 'std::vector<_Ty>::size_type (__thiscall
    > std::vector<_Ty>::* )(void) const' to 'int'
    > with
    > [
    > _Ty=Person *
    > ]
    >
    > Here are all the class definitions
    > *********************
    > #include <string>
    > using namespace std;
    > class Person
    > {
    > public:
    > Person(string nn = "default") : name(nn) {}
    > string getName() const
    > {
    > return name;
    > }
    > private:
    > string name;
    > };
    >
    > class Student : public virtual Person
    > {
    > public:
    > Student(string nn="default") : Person(nn) {}
    > };
    >
    > class Employee : public virtual Person
    > {
    > public:
    > Employee(string nn="default") : Person(nn) {}
    > };
    >
    > class TeachingAssistent : public Student, public Employee
    > {
    > public:
    > TeachingAssistent(string nn="default") : Person(nn) {}
    > };
    >
    > Here is main program
    > ****************
    > #include <vector>
    > #include "person.h"
    > #include <iostream>
    > using namespace std;
    >
    > int main()
    > {
    > vector<Person *> p;
    > p.push_back(new Student);
    > p.push_back(new Employee);
    > p.push_back(new TeachingAssistent);
    >
    > for(int i=0; i < p.size; i++)
    > cout << p->getName() << endl;
    >
    > return 0;
    > }
    >
    > Many thanks
    >
    > //Tony
    >
    >
     
    Ruwen Schnabel, Aug 16, 2005
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Schwab

    Multiple Inheritance size problem

    Jeff Schwab, Feb 17, 2004, in forum: C++
    Replies:
    3
    Views:
    541
    Rob Williscroft
    Feb 17, 2004
  2. Nitin Shukla

    Multiple Inheritance __slots__ problem

    Nitin Shukla, Dec 24, 2004, in forum: Python
    Replies:
    1
    Views:
    792
    Alex Martelli
    Dec 24, 2004
  3. Axel Straschil
    Replies:
    6
    Views:
    484
    Axel Straschil
    Apr 11, 2005
  4. Taki Jeden
    Replies:
    3
    Views:
    514
    Taki Jeden
    Jun 3, 2005
  5. Replies:
    2
    Views:
    367
  6. dl
    Replies:
    14
    Views:
    675
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Nov 12, 2006
  7. massimo s.
    Replies:
    14
    Views:
    553
    massimo s.
    Jun 28, 2007
  8. Rouslan Korneychuk
    Replies:
    8
    Views:
    850
    Rouslan Korneychuk
    Feb 10, 2011
Loading...