Невозможно неявно преобразовать тип ... со свойством - PullRequest
0 голосов
/ 19 июля 2011

Я пытался создать класс треугольника, который содержит три вектора 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, а возвращаемое значение vertex1 также является переменной Vector3.

Кто-нибудь может понять, почему я сталкиваюсь с этой проблемой?

Вот полный код для класса 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;
    }

}
}

Ответы [ 2 ]

2 голосов
/ 19 июля 2011

Вы объявляете GetVertex1 следующим образом:

public Vector3 GetVertex1

Это прекрасно, что вы хотите.Однако в классе isocahedron в методе уточнения у вас есть следующие фрагменты:

    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 значение Vector3, когда оно объявлено как TriXYZ.

Выясните, какие типы вещей должны быть, и вы должны быть отсортированы.:)

0 голосов
/ 19 июля 2011
  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 

эти строки переопределяют Vertex3 из Microsoft.Xna.Framework.Vector3 на TriXYZ, поэтому вы можете изменить имя объекта TriXYZ

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...