我正在尝试在 Unity 中使用PriorityQueue
C#。文档说它在 .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)
但返回一个错误:
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]
为什么它在 Unity 中不起作用?
编译失败的例子:
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
?
我的印象是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)
通常在我的代码中,我将变量声明为这样的函数的结果。它们被声明为基类,但具有派生的运行时类型,我想将它们传递给一个重载函数。
我如何以与使用涉及类层次结构的常规指针相同的方式重载函数?
今天,我在尝试回答这个帖子(关于检查是否可以构造三角形)时,遇到了一个奇怪的结果。
经测试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) 我有一个函数将两个LPCWSTR
s连接在一起,方法是将它们转换为wstring
s,添加它们,将其转换回来,然后返回该值(取自: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) 出于某种原因, 的值为final_sum
16 但我没有为该变量初始化值,这是为什么呢?不是应该开始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++ ×5
.net-6.0 ×1
c# ×1
double ×1
hierarchy ×1
inheritance ×1
namespaces ×1
overloading ×1
return ×1
std ×1
unique-ptr ×1