为什么PERSON不是ref级?

时间:2009-06-08 12:43:20

标签: class inheritance c++-cli ref

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

  

:错误C2811:'跑步者':不能   继承自'Person',ref类可以   只从遗传类或继承   接口类

我试过......

  1. 将mscorlib.dll添加到头文件中:#using..etc ...<> - 没用。
  2. 使Person成为一个抽象类 - 没有用(我很高兴,因为我想这是一个可实例化的类)
  3. 我先列出两个头文件。如果你需要,他们的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;
    }
    

1 个答案:

答案 0 :(得分:0)

你不应该放:

#using <mscorlib.dll>

位于头文件的顶部?我不确定这是不是可以解决这个问题。

尝试制作Personabstract