繁体   English   中英

C ++结构未编译...未正确初始化? 不正确使用它们?

[英]C++ Structs Not Compiling… Not Initialized Properly? Not using them right?

我正在尝试使用嵌套的结构/结构,经过几个小时的伪代码和尝试后,我提出的最终结果不起作用或不能编译。

我想取两个向量A和B,并将它们相互比较。 我设置了嵌套结构来读取向量的起点和终点,以及向量结构本身。 所以我想我可能在下面做了一些错误,但我被困了。

    #include <iostream>
    #include <cmath>
    #include <string>

    using namespace std;
struct Point    // Reads in three coordinates for point to make a three dimensional vector
{
    double x;
    double y;
    double z;
};
struct MathVector   // Struct for the start and end point of each vector.
{
    Point start;
    Point end;
};
Point ReadPoint()
{
    Point pt; // Letter distinguishes between vector A and vector B, or "letterA" and "letterB"
    double x, y, z;

    cout << "Please input the x-coordinate: " << endl;
    cin >> pt.x;
    cout << "Please input the y-coordinate: " << endl;
    cin >> pt.y;
    cout << "Please input the z-coordinate: " << endl;
    cin >> pt.z;

    return pt;
}
void DotProduct (MathVector letterA, MathVector letterB, double& a_times_b ) //formula to compute orthogonality
{
    a_times_b = (letterA.end.x - letterA.start.x)*(letterB.end.x - letterB.start.x) + (letterA.end.y - letterA.start.y)*(letterB.end.y - letterB.start.y) + (letterA.end.z - letterA.start.z)*(letterB.end.z - letterB.start.z);
}
int main()
{
    MathVector letterA;
    MathVector letterB;
    double a_times_b;

    letterA = ReadPoint();
    letterB = ReadPoint();

    DotProduct (letterA, letterB, a_times_b);

    cout << "The vector " << letterA << " compared with " << letterB << " ";
    if ( a_times_b == 0)
        cout << "is orthoganal." << endl;
    else
        cout << "is not orthoganal." << endl;

    return 0;
}

一个问题是您的ReadPoint的返回类型是Point ,但是您正在返回MathVector一个实例。 此外,您还读取了最终忽略的变量的输入。

你应该把ReadPoint写成:

Point ReadPoint()
{
    Point p;
    cout << "Please input the x-coordinate: " << endl;
    cin >> p.x;
    cout << "Please input the y-coordinate: " << endl;
    cin >> p.y;
    cout << "Please input the z-coordinate: " << endl;
    cin >> p.z;
    return p;
}

或者更好的版本:

Point ReadPoint()
{
    Point p;
    cout << "Please enter point-coordinate : " << endl;
    cin >> p.x >> p.y >> p.z; //example input : 1 2 3 
    return p;
}

或者,更好的是,重载>>运算符为:

std::istream & operator>>(std::istream & in, Point & p)
{
    cout << "Please enter point-coordinate : " << endl;
    return cin >> p.x >> p.y >> p.z; //example input : 1 2 3 
}

//Use this as
Point pointA, pointB;
cin >> pointA >> pointB;

现在阅读一本好的C ++书。 如果您已经阅读了一个,那么请确保它真的很好。 这是非常好的C ++书籍,各级的列表:

  1. ReadPoint返回MathVector类型的字母而不是Point
  2. 你没有重载operator <<来告诉它如何处理MathVector对象
Point ReadPoint()
{
   MathVector letter; // Letter distinguishes between vector A and vector B, or "letterA" and "letterB"
    double x, y, z;

   cout << "Please input the x-coordinate: " << endl;
   cin >> x;
   cout << "Please input the y-coordinate: " << endl;
   cin >> y;
   cout << "Please input the z-coordinate: " << endl;
   cin >> z;

   return letter;

}

你没有解释你正在尝试做什么或者你得到了什么错误,但这段代码毫无意义。 你有三个变量, xyz 您可以使用从用户获得的值填充它们。 然后你不对这些变量做任何事情并返回由默认构造函数创建的MathVector ,即使你说你要返回一个Point 这没什么意义。

letterAletterB的类型为MathVector

 MathVector letterA;
        MathVector letterB;
        double a_times_b;

        letterA = ReadPoint();
        letterB = ReadPoint();

你应该创建另一个方法来读取Mathvector ..就像你在使用Point

并在方法ReadPoint

返回类型必须是Point ..如果你读点,那么在这里做计算来创建MathVector的对象go tet startpointendpoint format。

'operator ='错误不匹配意味着没有将MathVector分配给Point的功能。 您正在调用ReadPoint(),它返回一个Point并尝试将返回的值分配给MathVector类型的变量。 编译器无法自动创建“转换”功能。 你必须自己提供一个。 也许你的意思是

letterA.start = ReadPoint();
letterA.end   = ReadPoint();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM