无法在 CLR C++ 中创建虚拟 class
Can't create virtual class in CLR C++
我正在处理 MSVS 2010 中的简单程序,它是 Windows 表单应用程序,突然遇到了一个非常奇怪的问题:我无法在 class.[=13= 中创建虚函数]
代码如下:
#pragma once;
#include <string>
using namespace std;
using namespace System;
class Reptile
{
private:
char *diet;
string title;
double weight;
int lifespan;
char sex;
public:
char* getDiet();
bool setDiet(char* newDiet);
string getTitle();
bool setTitle(string newTitle);
double getWeight();
bool setWeight(double newWeight);
int getLifespan();
bool setLifeSpan(int newLifespan);
char getSex();
void setSex(char newSex);
virtual void Show(void);
virtual void Voice(void);
~Reptile();
Reptile(); };
它抛出这样的错误:
Reptile.obj : error LNK2028: unresolved token (0A00000E) "public: virtual void __clrcall Reptile::Voice(void)" (?Voice@Reptile@@$$FUAMXXZ) referenced in function "void __clrcall dynamic initializer for 'const Reptile::vftable'''(void)" (???__E??_7Reptile@@6B@@@YMXXZ@?A0xc2bc2ccd@@$$FYMXXZ)
我真的不知道怎么处理,因为我对 clr 之类的不是很熟悉。
包括 Show()、Voice()、Reptile() 和 ~Reptile() 的主体,它将起作用。像这样:
virtual void Show() {};
virtual void Voice() {};
Reptile() {};
~Reptile() {};
您可以在下面看到一个工作示例:
#include <iostream>
#include <string>
using namespace std;
class Reptile
{
private:
char* diet;
string title;
double weight;
int lifespan;
char* sex;
public:
char* getDiet()
{
return diet;
};
void setDiet(char* newDiet)
{
diet = newDiet;
};
string getTitle()
{
return title;
}
void setTitle(string newTitle)
{
title = newTitle;
}
double getWeight()
{
return weight;
}
double setWeight(double newWeight)
{
weight = newWeight;
}
int getLifespan()
{
return lifespan;
}
void setLifeSpan(int newLifespan)
{
lifespan = newLifespan;
}
char* getSex()
{
return sex;
}
void setSex(char* newSex)
{
sex = newSex;
}
// make them pure virtual functions
virtual void Show() = 0;
virtual void Voice() = 0;
Reptile() {};
~Reptile() {};
};
class Dinosaur : public Reptile
{
public:
virtual void Show()
{
cout << "Showing dino" << endl;
}
virtual void Voice()
{
cout << "Rawrrrrr!" << endl;
}
};
int main()
{
Reptile *dino1 = new Dinosaur();
dino1->setSex("M");
dino1->setTitle("T-Rex");
cout << "Type: " << dino1->getTitle() << endl;
cout << "Sex: " << dino1->getSex() << endl;
dino1->Show();
dino1->Voice();
delete dino1;
}
我正在处理 MSVS 2010 中的简单程序,它是 Windows 表单应用程序,突然遇到了一个非常奇怪的问题:我无法在 class.[=13= 中创建虚函数]
代码如下:
#pragma once;
#include <string>
using namespace std;
using namespace System;
class Reptile
{
private:
char *diet;
string title;
double weight;
int lifespan;
char sex;
public:
char* getDiet();
bool setDiet(char* newDiet);
string getTitle();
bool setTitle(string newTitle);
double getWeight();
bool setWeight(double newWeight);
int getLifespan();
bool setLifeSpan(int newLifespan);
char getSex();
void setSex(char newSex);
virtual void Show(void);
virtual void Voice(void);
~Reptile();
Reptile(); };
它抛出这样的错误:
Reptile.obj : error LNK2028: unresolved token (0A00000E) "public: virtual void __clrcall Reptile::Voice(void)" (?Voice@Reptile@@$$FUAMXXZ) referenced in function "void __clrcall dynamic initializer for 'const Reptile::vftable'''(void)" (???__E??_7Reptile@@6B@@@YMXXZ@?A0xc2bc2ccd@@$$FYMXXZ)
我真的不知道怎么处理,因为我对 clr 之类的不是很熟悉。
包括 Show()、Voice()、Reptile() 和 ~Reptile() 的主体,它将起作用。像这样:
virtual void Show() {};
virtual void Voice() {};
Reptile() {};
~Reptile() {};
您可以在下面看到一个工作示例:
#include <iostream>
#include <string>
using namespace std;
class Reptile
{
private:
char* diet;
string title;
double weight;
int lifespan;
char* sex;
public:
char* getDiet()
{
return diet;
};
void setDiet(char* newDiet)
{
diet = newDiet;
};
string getTitle()
{
return title;
}
void setTitle(string newTitle)
{
title = newTitle;
}
double getWeight()
{
return weight;
}
double setWeight(double newWeight)
{
weight = newWeight;
}
int getLifespan()
{
return lifespan;
}
void setLifeSpan(int newLifespan)
{
lifespan = newLifespan;
}
char* getSex()
{
return sex;
}
void setSex(char* newSex)
{
sex = newSex;
}
// make them pure virtual functions
virtual void Show() = 0;
virtual void Voice() = 0;
Reptile() {};
~Reptile() {};
};
class Dinosaur : public Reptile
{
public:
virtual void Show()
{
cout << "Showing dino" << endl;
}
virtual void Voice()
{
cout << "Rawrrrrr!" << endl;
}
};
int main()
{
Reptile *dino1 = new Dinosaur();
dino1->setSex("M");
dino1->setTitle("T-Rex");
cout << "Type: " << dino1->getTitle() << endl;
cout << "Sex: " << dino1->getSex() << endl;
dino1->Show();
dino1->Voice();
delete dino1;
}