Hi. Can anyone tell me why the VC++ compiler tells me that Person is NOT a ref class. And because of that, that Runner cannout inherit from it...thanks...
error C2811: 'Runner' : cannot inherit from 'Person', a ref class can only inherit from a ref class or interface class
1>..\..\exercise\exercise\Runner.cpp(5) : error C2614: 'Runner' : illegal member initialization: 'Person' is not a base or member
1>..\..\exercise\exercise\Runner.cpp(11) : error C2614: 'Runner' : illegal member initialization: 'Person' is not a base or member
1>..\..\exercise\exercise\Runner.cpp(24) : error C2664: 'Person::operator =' : cannot convert parameter 1 from 'Runner' to 'const Person &'
1> An object from the gc heap (a dereferenced gc pointer) cannot be converted to a native reference
1>..\..\exercise\exercise\Runner.cpp(39) : error C2664: 'Person::operator ==' : cannot convert parameter 1 from 'Runner' to 'const Person &'
1> An object from the gc heap (a dereferenced gc pointer) cannot be converted to a native reference
1>Person.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#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);
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#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;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#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);
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#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;
}
|