0

我不明白为什么编译器认为 PERSON 不是 ref 类:

: 错误 C2811: 'Runner' : 不能从'Person'继承,引用类只能从引用类或接口类继承

我试过了....

  1. 将 mscorlib.dll 添加到头文件中:#using..etc...<> - 不起作用。
  2. 使 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); 
};

跑步者.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);
};

人.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;
}

跑步者.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;
}
4

1 回答 1

0

你不应该把:

#using <mscorlib.dll>

在头文件的顶部?老实说,我不确定这是否能解决问题。

试着让你的Personabstract

于 2009-06-08T12:51:44.823 回答