Help! Class problem.
Hey guys, im trying to create a priority queue in C#. In my case i gonna need a queue of vector3, soo i start creating a class called Charge with have a vector3 and a double for priority, then i create the class PriorityQueue that is a list of charge. You can follow the code below:
public class Charge{
public Vector3 vetor = new Vector3();
public double priority = new double ();
public Charge(Vector3 a, double b){
vetor = a;
priority = b;
}
}
public class PriorityQueue<Charge> where Charge : new (){
private List<Charge> elements = new List<Charge>();
Charge t ;
public int Count
{
get { return elements.Count; }
}
public void Enqueue(Vector3 item, double priority)
{
t = new Charge(item, priority);
elements.Add(t);
}
public Charge Dequeue()
{
int bestIndex = 0;
Charge bestItem = new Charge();
for (int i = 0; i < elements.Count; i++) {
if (elements[i].priority < elements[bestIndex].priority) {
bestIndex = i;
}
}
bestItem = elements[bestIndex].vetor;
elements.RemoveAt(bestIndex);
return bestItem;
}
}
However i geting 3 errors: error CS0417: Charge': cannot provide arguments when creating an instance of a variable type error CS1061: Type
Charge' does not contain a definition for priority' and no extension method
priority' of type Charge' could be found. error CS1061: Type
Charge' does not contain a definition for vetor' and no extension method
vetor' of type `Charge' could be found.
The thing is i don't get it, why i can't access the methods of the class i just created? did i made something wrong? can someone explain me plis. Thx
Answer by Chikari · Feb 13, 2017 at 02:22 PM
using UnityEngine;
using System.Collections.Generic;
public class Charge{
public Vector3 vetor;
public double priority;
public Charge(Vector3 a, double b){
vetor = a;
priority = b;
}
}
public class PriorityQueue<T> where T : Charge{
private List<Charge> elements = new List<Charge>();
Charge t ;
public int Count
{
get { return elements.Count; }
}
public void Enqueue(Vector3 item, double priority)
{
t = new Charge(item, priority);
elements.Add(t);
}
public Vector3 Dequeue()
{
int bestIndex = 0;
Vector3 bestItem = Vector3.zero;
for (int i = 0; i < elements.Count; i++) {
if (elements[i].priority < elements[bestIndex].priority) {
bestIndex = i;
}
}
bestItem = elements[bestIndex].vetor;
elements.RemoveAt(bestIndex);
return bestItem;
}
}
Your answer
Follow this Question
Related Questions
MonoDevelop doesn't recognize Priority_Queue library 0 Answers
An object reference is required to access non-static member `UnityEngine.Behaviour.enabled' 0 Answers
UnityScript: BCE0048: Type 'ScriptFileName' does not support slicing. 0 Answers
Action queue/list for NPCs in point and click adventure. 0 Answers