为什么 PERSON 不是 ref 类?
我不明白为什么编译器认为 PERSON 不是引用类:
:错误 C2811:“跑步者”:不能 继承自“Person”,引用类可以 仅继承自 ref 类或 接口类
我尝试过的
- ...将 mscorlib.dll 添加到头文件中:#using..etc...<> - 没用。
- 让 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....
- adding mscorlib.dll to the header files: #using..etc...<> - didn't work.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不应该把:
放在头文件的顶部吗? 老实说,我不确定这是否能解决问题。
尝试使您的
Person
类抽象
。Shouldn't you be putting:
at the top of your header files? I'm not sure if that would fix the issue to be honest.
Try making your
Person
classabstract
.