小编Arn*_*rah的帖子

如何在没有类型代码的情况下普遍引用类的实例?

我正在制作一个基于文本的RPG,其中我有一个抽象Item类.从这个Item班,我有课Weapon,Potion,Key,和Armor.主要角色类,Protagonist使用这些项目并具有功能doItemEffect(Item*).我如何doItemEffect(Item*)以一种普遍引用所有项目的方式实施?为了更好地表达我的问题,如果我不清楚,这里有一个使用非常难看的解决方案的例子.

class Protagonist
{
public:
    void doItemEffect(Item* it)
    {
        switch(it->getType()) //<-- The type is an enum class
        {
        case ItemType::WEAPON:
            attackOpponent(it.getAttackPower()); //If it is a weapon it would have this function
            break;
        case ItemType::POTION:
            heal(it.getHealPower()); //If it is a weapon it would have this function
            break;
        case ItemType::KEY:
            //..Code..
            break;
        case ItemType::ARMOR:
            //More Code...
            break;
     }
};
Run Code Online (Sandbox Code Playgroud)

并且有两个类的示例PotionWeapon(类的类型是 …

c++ oop polymorphism c++11

1
推荐指数
1
解决办法
108
查看次数

C#:HTML电子邮件模板字体颜色不起作用

我基本上试图从我的C#代码创建一个HTML电子邮件消息.一切都按预期工作,但当我使用字体颜色时,我收到了收件箱中的空白字段.

我的代码片段:

string type = "<strong><style=\"color: red; \">" + detail.ToString() + "</style>
</strong>";
Run Code Online (Sandbox Code Playgroud)

当我这么做时,类型显示正确,键入= detail.ToString().但是当我添加样式时不起作用.代码只显示空白字.

有任何想法吗?

html css c#

0
推荐指数
1
解决办法
110
查看次数

如何在某些文字下方移动边框?

假设我有一个这样的片段.

h1 {
  border-bottom: 10px black solid;
}
Run Code Online (Sandbox Code Playgroud)
<h1>My name is Apujay.</h1>
Run Code Online (Sandbox Code Playgroud)

如何将文本下方的边框向下移动?

html css border

0
推荐指数
1
解决办法
88
查看次数

当我们需要在现实生活中将二进制转换为十六进制时

我是一名学生,正在研究二进制和十六进制.我想知道为什么我们需要将二进制数转换为十六进制数.它在现实生活中的用途是什么?

c++ binary hex

0
推荐指数
1
解决办法
81
查看次数

为什么使用const的方法而不是没有const的第一个方法

void print( int & a ){ cout << a << endl; }
void print( const int & a ){ cout << 2*a << endl; }
void print( float a ){ cout << 3*a << endl; }
Run Code Online (Sandbox Code Playgroud)

为什么2输出print(1)

c++ methods const function overload-resolution

-3
推荐指数
1
解决办法
82
查看次数

我已经在c ++中实现了合并排序.它没有错误但不知何故它不起作用

下面给出的是c ++中的合并排序实现.我使用过visual studio代码.它需要输入,但只是停止而不显示任何错误.我认为它可能在某处出现了分段错误,但我无法弄明白.

#include<iostream>

using namespace std;

void merge(int arr[], int beg, int mid, int end) //function to merge the arrays
{
int i = beg;
int j = mid +1;
int k, index = 0;
int temp[100];
while(i <= mid && j <= end)
{
    if(arr[i] < arr[j])
    {
        temp[index] = arr[i];
        i++;
    }
    else
    {
        temp[index] = arr[j];
        j++;

    }
    index++;
}
while(i <= mid)
{
    temp[index] = arr[i];
    i++;
    index++;
}

while(j <= end)
{
    temp[index] = …
Run Code Online (Sandbox Code Playgroud)

c++ mergesort visual-studio-code

-4
推荐指数
1
解决办法
85
查看次数

为什么我的srand会返回所有结果?

这是一个简单的文字游戏的开始.在游戏中,你应该四处寻找地下城并收集文物.我习惯srand(time(0))做一些事情,比如找到要进入的阶段,攻击,以及你找到的项目,我已经在编程中走得很远,但我已经遇到了问题.我的rand()返回所有结果.当我运行游戏时(这不是完整的代码,顺便说一句),它会返回"你进入了一个地牢!","哦不,敌人已经到了!",并且"你找到了一件神器!

void mainScreen()
{
    srand(time(0));
    cout << "Health: \n";
    cout << health;
    cout << endl;
    _sleep(500);
    cout << "Inventory: \n";
    cout << inventory;
    cout << endl;
    _sleep(500);
    cout << "Gold: \n";
    cout << gold;
    cout << endl;
    _sleep(500);
    cout << "Artifacts: \n";
    cout << artifacts;
    cout << endl;
    _sleep(500);
    cout << "Rolling the dice of fate... \n";
    int diceRoll = 1 + (rand() % 10);
    if (diceRoll = 1, 2, 3, 4, 5, 6) {
        cout << "You …
Run Code Online (Sandbox Code Playgroud)

c++ random srand

-7
推荐指数
1
解决办法
44
查看次数