无法隐式转换类型..具有属性
本文关键字:属性 类型 转换 | 更新日期: 2023-09-27 18:02:26
我一直在尝试建立一个包含三个Vector3和一个整数的三角形类。
这是类构造函数:(不确定这是正确的术语,我是一个业余爱好者(
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Icosahedron_Test
{
class TriXYZ
{
Vector3 vertex1;
Vector3 vertex2;
Vector3 vertex3;
int depth;
float material1; // float for first material value amount (in %) deals with blending
float material2; // float for second material value amount (in %) deals with blending
Vector3 vValue; // place holder for vertex properties "set"
int dValue; // place holder for depth properties "set"
public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth)
{
vertex1 = pos1;
vertex2 = pos2;
vertex3 = pos3;
depth = tDepth;
}
这就是我设置属性的方式:
public Vector3 GetVertex1
{
get { return vertex1; }
set { vertex1 = vValue; }
}
这就是我从另一个类调用属性的方式:
Vector3 cVertex1;
TriXYZ cTriangle = triangleList[listPos]; // triangleList is a TriXYZ[] array
.
.
.
cVertex1 = cTriangle.GetVertex1;
我得到的错误是:
Cannot implicitly convert type 'Microsoft.Xna.Framework.Vector3' to 'Icosahedron_Test.TriXYZ'
我理解这个错误通常意味着什么,本质上是我试图将字符串分配给一个整数或类似的东西。我不明白的是为什么我在这里看到错误。变量cVertex1是一个Vector3变量,而verex1的返回值也是一个Vector 3变量。
有人明白我为什么会遇到这个问题吗?
以下是我迄今为止开发的TriXyZ类和Icosahdron类的完整代码:
TriXYZ:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Icosahedron_Test
{
class TriXYZ
{
Vector3 vertex1;
Vector3 vertex2;
Vector3 vertex3;
int depth;
float material1; // float for first material value amount (in %) deals with blending
float material2; // float for second material value amount (in %) deals with blending
Vector3 vValue; // place holder for vertex properties "set"
int dValue; // place holder for depth properties "set"
public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth)
{
vertex1 = pos1;
vertex2 = pos2;
vertex3 = pos3;
depth = tDepth;
}
public TriXYZ(Vector3 pos1, Vector3 pos2, Vector3 pos3, int tDepth, float tMaterial1, float tMaterial2)
{
vertex1 = pos1;
vertex2 = pos2;
vertex3 = pos3;
depth = tDepth;
material1 = tMaterial1;
material2 = tMaterial2;
}
// public access to triangle data, read-write
public Vector3 GetVertex1
{
get { return vertex1; }
set { vertex1 = vValue; }
}
public Vector3 GetVertex2
{
get { return vertex2; }
set { vertex2 = vValue; }
}
public Vector3 GetVertex3
{
get { return vertex3; }
set { vertex3 = vValue; }
}
public int GetDepth
{
get { return depth; }
set { depth = dValue; }
}
public Vector3 Midpoint(Vector3 pos1, Vector3 pos2, int tDepth)
{
Vector3 midpoint; // returned midpoint between the two inputted vectors
//PLACEHOLDER
return midpoint;
}
}
}
和ICOSAHDRON:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Icosahedron_Test
{
class Icosahedron
{
int radius; // radius of the planet
int refinement; // number of times to refine the traingles
int faces = 20;
Vector3[] basePositions; // Vertex points for three defining rectangles
TriXYZ[] vertices; // Vertex points for triangles which define the spherical surface
public Icosahedron(int tRadius, int tRefinement, TriXYZ[] tVertices)
{
radius = tRadius;
refinement = tRefinement;
vertices = tVertices;
}
protected void Initialize()
{
double t = radius*((1+Math.Sqrt(5))/2);
Vector3[] basePositions =
{
//First Rectangle
Vector3.Normalize(new Vector3(-radius, (float)t, 0)),
Vector3.Normalize(new Vector3(radius, (float)t, 0)),
Vector3.Normalize(new Vector3(-radius, (float)-t, 0)),
Vector3.Normalize(new Vector3(radius, (float)-t, 0)),
//Seconds Rectangle
Vector3.Normalize(new Vector3(0, -radius, (float)t)),
Vector3.Normalize(new Vector3(0, radius, (float)t)),
Vector3.Normalize(new Vector3(0, -radius, (float)-t)),
Vector3.Normalize(new Vector3(0, radius, (float)-t)),
//Third Rectangle
Vector3.Normalize(new Vector3((float)t, 0, -radius)),
Vector3.Normalize(new Vector3((float)t, 0, radius)),
Vector3.Normalize(new Vector3((float)-t, 0, -radius)),
Vector3.Normalize(new Vector3((float)-t, 0, radius))
};
TriXYZ[] vertices =
{
new TriXYZ(basePositions[0], basePositions[11], basePositions[5], 1),
new TriXYZ(basePositions[0], basePositions[5], basePositions[1], 1),
new TriXYZ(basePositions[0], basePositions[1], basePositions[7], 1),
new TriXYZ(basePositions[0], basePositions[7], basePositions[10], 1),
new TriXYZ(basePositions[0], basePositions[10], basePositions[11], 1),
new TriXYZ(basePositions[1], basePositions[5], basePositions[9], 1),
new TriXYZ(basePositions[5], basePositions[11], basePositions[4], 1),
new TriXYZ(basePositions[11], basePositions[10], basePositions[2], 1),
new TriXYZ(basePositions[10], basePositions[7], basePositions[6], 1),
new TriXYZ(basePositions[7], basePositions[1], basePositions[8], 1),
new TriXYZ(basePositions[3], basePositions[9], basePositions[4], 1),
new TriXYZ(basePositions[3], basePositions[4], basePositions[2], 1),
new TriXYZ(basePositions[3], basePositions[2], basePositions[6], 1),
new TriXYZ(basePositions[3], basePositions[6], basePositions[8], 1),
new TriXYZ(basePositions[3], basePositions[8], basePositions[9], 1),
new TriXYZ(basePositions[4], basePositions[9], basePositions[5], 1),
new TriXYZ(basePositions[2], basePositions[4], basePositions[11], 1),
new TriXYZ(basePositions[6], basePositions[2], basePositions[10], 1),
new TriXYZ(basePositions[8], basePositions[6], basePositions[7], 1),
new TriXYZ(basePositions[9], basePositions[8], basePositions[1], 1),
};
}
private TriXYZ[] Refinement(TriXYZ[] rVertices, int rRefinement)
{
TriXYZ[] tVertices; // Temp list of triangles
int cDepth = 1; // current depth integer
TriXYZ vertex1; // position of first vertex of base triangle
TriXYZ vertex2; // position of second vertex of base triangle
TriXYZ vertex3; // position of third vertex of base triangle
int tDepth; // depth of the current triangle
TriXYZ mid1; // position of first midpoint
TriXYZ mid2; // position of second midpoint
TriXYZ mid3; // position of third midpoint
int listPos = 0; // base list position integer
int nListPos = 0; // new list position integer
int cRefine = 1; // current refinement iteration
while(cRefine < rRefinement) // loop until the icosphere has been refined the inputted number of times
{
tVertices = null;
foreach (TriXYZ i in rVertices)
{
TriXYZ cTriangle = tVertices[listPos];
vertex1 = cTriangle.GetVertex1;
vertex2 = cTriangle.GetVertex2;
vertex3 = cTriangle.GetVertex3;
tDepth = tVertices[listPos].GetDepth;
mid1 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
mid2 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
mid3 = TriXYZ.Midpoint(vertex1, vertex2, tDepth);
}
}
return rVertices;
}
}
}
您这样声明GetVertex1:
public Vector3 GetVertex1
这很好,如果这是你想要的。然而,在Refinement方法中的等容面体类中,您有以下片段:
TriXYZ vertex1; // position of first vertex of base triangle
TriXYZ vertex2; // position of second vertex of base triangle
TriXYZ vertex3; // position of third vertex of base triangle
...
...
vertex1 = cTriangle.GetVertex1;
vertex2 = cTriangle.GetVertex2;
vertex3 = cTriangle.GetVertex3;
因此,当vertex1被声明为TriXYZ时,它试图将其设置为Vector3。
弄清楚事情应该是什么类型,你应该被分类。:(
TriXYZ vertex1; // position of first vertex of base triangle
TriXYZ vertex2; // position of second vertex of base triangle
TriXYZ vertex3; // position of third vertex of base triangle
这些行将"Microsoft.Xna.Framework.Vvector3"的Vertex3覆盖为TriXYZ,因此您可以更改TriXYZ 的对象名