为什么 PERSON 不是 ref 类?

发布于 2024-07-23 05:37:52 字数 3221 浏览 9 评论 0原文

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

:错误 C2811:“跑步者”:不能 继承自“Person”,引用类可以 仅继承自 ref 类或 接口类

我尝试过的

  1. ...将 mscorlib.dll 添加到头文件中:#using..etc...<> - 没用。
  2. 让 Person 成为一个抽象类 - 不起作用(我很高兴,因为我想象它是一个可实例化的??类)

我首先列出了两个头文件。 如果您需要的话,稍后会列出他们的 cose。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2024-07-30 05:37:52

你不应该把:

#using <mscorlib.dll>

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

尝试使您的 Person抽象

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文