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

时间:2011-11-25 05:17:13

标签: c++ struct

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

我想采用两个向量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;
}

5 个答案:

答案 0 :(得分:2)

问题在于您的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 :(得分:0)

  1. ReadPoint返回MathVector类型的字母,而不是Point
  2. 您没有重载运算符&lt;&lt;告诉它如何处理MathVector对象

答案 2 :(得分:0)

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。这没什么意义。

答案 3 :(得分:0)

letterAletterB的类型为MathVector

 MathVector letterA;
        MathVector letterB;
        double a_times_b;

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

您应该创建另一种方法来阅读Mathvector ..正如您使用Point一样。

和方法ReadPoint

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

答案 4 :(得分:0)

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

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