小编sil*_*fox的帖子

无法在 Unity 中使用 C# .NET 6 PriorityQueue

我正在尝试在 Unity 中使用PriorityQueueC#。文档说它在 .NET 6 的命名空间中受支持System.Collections.Generic

我试过了:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Test : Monobehaviour
{
    void Start()
    {
        var queue = new PriorityQueue<int, int>();
    }
}
Run Code Online (Sandbox Code Playgroud)

但返回一个错误:

The type or namespace name 'PriorityQueue<,>' could not be found (are
you missing a using directive or an assembly reference?)
[Assembly-CSharp]
Run Code Online (Sandbox Code Playgroud)

我检查了 VS Code 中的 .NET 版本: 在此输入图像描述

为什么它在 Unity 中不起作用?

c# priority-queue unity-game-engine .net-6.0

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

当派生类方法不可行时,为什么 C++ 重载决策不查看基类方法?

编译失败的例子:

class A{
 public:
  int f(int a) {return a;}
};

class B: public A {
 public:
  int f(int a, int b) {return a + b;}
};

int calculation(int num) {
    B b;
    return b.f(num);
}
Run Code Online (Sandbox Code Playgroud)

在调用站点b.f(num)gcc给出以下错误消息:

错误:没有匹配的函数可供调用B::f(int&)

基类有一个可行的候选者,但由于某种原因编译器不会考虑它。

如果我将调用重写为b.A::f(num),那么它就可以正常工作。我不明白为什么这A::是必要的。为什么 的A::f重载解析逻辑不将其视为可行的候选者b.f

c++ inheritance overload-resolution

5
推荐指数
2
解决办法
369
查看次数

基于unique_ptr参数指针类型的重载函数

我的印象是unique_ptr可以以与普通指针相同的方式推断类层次结构,但是当我尝试重载这样的函数时:

void func(unique_ptr<Derived1>& d1);
void func(unique_ptr<Derived2>& d2);
Run Code Online (Sandbox Code Playgroud)

然后调用这样的函数之一:

unique_ptr<Base> b = make_unique<Derived1>();
func(b);    
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说no instance of overloaded function "func" matches the argument list。b 的运行时类型是Derived1,所以我预计会调用第一个重载。

此外,当我unique_ptr从函数返回 a 时,编译器能够将派生类(?不确定合适的术语)转换为基类,以便像这样工作:

unique_ptr<Base> create(){
    return make_unique<Derived1>();
}
Run Code Online (Sandbox Code Playgroud)

通常在我的代码中,我将变量声明为这样的函数的结果。它们被声明为基类,但具有派生的运行时类型,我想将它们传递给一个重载函数。

我如何以与使用涉及类层次结构的常规指针相同的方式重载函数?

c++ overloading hierarchy unique-ptr

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

为什么“使用命名空间 std;” 在 C++ 中处理双打时给出不同的结果?

今天,我在尝试回答这个帖子(关于检查是否可以构造三角形)时,遇到了一个奇怪的结果。

经测试15.15 35.77 129.07,这段代码:

#include <iostream>
using namespace std;

const double e = 0.000001;

void f(double a, double b, double c)
{
    if (abs(180 - (a+b+c)) > e) {cout << "test"; }
}

int main()
{
    double a,b,c; cin >> a >> b >> c;
    f(a,b,c);
}
Run Code Online (Sandbox Code Playgroud)

test正常打印,而这个:

#include <iostream>
const double e = 0.000001;

void f(double a, double b, double c)
{
    if (abs(180 - (a+b+c)) > e) {std::cout << "test"; }
}

int …
Run Code Online (Sandbox Code Playgroud)

c++ double namespaces std

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

函数完美运行,但返回后更改值

我有一个函数将两个LPCWSTRs连接在一起,方法是将它们转换为wstrings,添加它们,将其转换回来,然后返回该值(取自:How to concatenate a LPCWSTR?

LPCWSTR addLPCWSTRs(LPCWSTR lpcwstr1, LPCWSTR lpcwstr2) {
    //Add the strings together
    std::wstring wstringCombined = std::wstring(lpcwstr1) + std::wstring(lpcwstr2);
    //Convert from wstring back to LPCWSTR
    LPCWSTR lpcwstrCombined = wstringCombined.c_str();
    return lpcwstrCombined;
}

    LPCWSTR BaseURL = L"https://serpapi.com/search.json?tbm=isch?q=";
    LPCWSTR imageQuery = L"baby+animals";

   LPCWSTR URL = addLPCWSTRs(BaseURL, imageQuery);

Run Code Online (Sandbox Code Playgroud)

在 return 语句之前,lpcwstrCombined值是正确的,当我在 return 语句之前中断时,调试器显示值也是正确的。

正确的值应该是:

正确值

当我在结尾的花括号处中断时,该值lpcwstr会在1-5来自其他语言的随机符号之前变成一堆正方形,并且它总是在变化。

例子:

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

这不需要更改任何代码,只需重置调试器并再次运行即可。我已经对此进行了数小时的研究,到目前为止还没有发现任何东西。数组的一个有点类似的问题据说使用指针而不是面值,但这并没有什么区别。为什么变量一返回就在函数外改变值??

编辑: 阅读评论后,我将其更改为:

std::wstring addLPCWSTRs(LPCWSTR lpcwstr1, LPCWSTR lpcwstr2) {
    //Add the strings together …
Run Code Online (Sandbox Code Playgroud)

c++ return

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

即使我将其留空,C++ 也会初始化变量的值,为什么会发生这种情况?

出于某种原因, 的值为final_sum16 但我没有为该变量初始化值,这是为什么呢?不是应该开始0吗?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    int n, sum, final_sum;

    cout<<"ff: "<<final_sum<<endl;
    cout<<"Input the value for n: ";
    cin>>n;

    for(int i=1; i<=n; i++){
        sum += i;
        final_sum += sum;
        cout<<"sum: "<<sum<<endl;
        cout<<"final sum: "<<final_sum<<endl<<endl;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ initialization

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