views:

317

answers:

1

I don't understand why compiler thinks PERSON is NOT a ref class:

: error C2811: 'Runner' : cannot inherit from 'Person', a ref class can only inherit from a ref class or interface class

I tried....

  1. adding mscorlib.dll to the header files: #using..etc...<> - didn't work.
  2. making Person an abstract class - didn't work (Im glad since I imagined that to be a instantiatable?? class)

I list the two header files first. Then their cose is listed later if you need it.

PERSON.H

#pragma once
using namespace System;

ref class Person
{
private:
    void copy_ptr_data(Person%);
    void delete_ptr_data(void);
public:
    property String^ Name;
    property String^ Age;

    Person(String^ name, String^ age);
    Person(Person%);
    Person% operator= (Person%);
    bool operator==(Person%);
    bool operator!=(Person%);
    virtual ~Person(void); 
};

RUNNER.H

#pragma once
#include "Person.h"

using namespace System;

ref class Runner : public Person
{
private:
    void copy_ptr_data(Runner%);
    void delete_ptr_data(void);
public:
    property String^ Time;
    property String^ Rank;

    Runner(String^ name, String^ age, String^ time);
    Runner(Runner%);
    Runner% operator= (Runner%);
    bool operator==(Runner%);
    bool operator!=(Runner%);
    ~Runner(void);
};

PERSON.CPP

#include "StdAfx.h"
#include "Person.h"

Person::Person(String^ name, String^ age) 
{
    Name = name;
    Age = age; 
}

Person::Person(Person% p)
{
    Name = p.Name;
    Age = p.Age;

    copy_ptr_data(p);
}

Person% Person::operator= (Person% p)
{
    // prevent self-assignment
    if (this == %p) {
     return *this;
    }

    // deallocate/reallocate/assign dynamic memory
    delete_ptr_data();
    copy_ptr_data(p);

    // assign non-dynamic memory
    Name    = p.Name;
    Age  = p.Age;
    return *this;
}

bool Person::operator==(Person% p)
{
    if ((Name == p.Name) &&
     (Age == p.Age))
     return 1;

    return 0; 
}

bool Person::operator!=(Person% p)
{
return !(Person::operator==(p));
}

Person::~Person(void) 
{
    delete_ptr_data();
}

void Person::copy_ptr_data(Person% p)
{
    return;
}

void Person::delete_ptr_data()
{
    return;
}

RUNNER.CPP

#include "StdAfx.h"
#include "Runner.h"

Runner::Runner(String^ name, String^ age, String^ time) : Person(name, age)      
{
    Time = time;
    Rank = nullptr;  
}

Runner::Runner(Runner% r) : Person(r) 
{
    Time = r.Time;
    Time = r.Rank;

    copy_ptr_data(r);
}

Runner% Runner::operator= (Runner% r)
{ 
    // handle self assignment
    if (this == %r) return *this;

    // handle base class portion
    Person::operator=(r);      

    // handle dynamic portion
    delete_ptr_data();
    copy_ptr_data(r);

    // handle non-dynamic portion
    Time = r.Time;
    Rank    = r.Rank;

    return *this;
}

bool Runner::operator==(Runner% r)
{
    if ((Person::operator==(r)) &&
     (Time == r.Time)  &&
     (Rank == r.Rank))
     return 1;

    return 0; 
}

bool Runner::operator!=(Runner% r)
{
return !(Runner::operator==(r));
}

Runner::~Runner(void) 
{
}

void Runner::copy_ptr_data(Runner% r)
{
    return;
}


void Runner::delete_ptr_data()
{
    return;
}
A: 

Shouldn't you be putting:

#using <mscorlib.dll>

at the top of your header files? I'm not sure if that would fix the issue to be honest.

Try making your Person class abstract.

OJ
Nope. neither of those worked. But thanks. I'm glad the abstract wasn't needed as I wanted Person to be able to be instantiated by other people. I swear I am the ONLY PERSON ON EARTH who gets compiler messages like these ;)