简体   繁体   English

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

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

I am -trying- to use nested structs/structures, and after several hours of pseudocode and attempts, the final result that I come up with doesn't work or doesn't compile. 我正在尝试使用嵌套的结构/结构,经过几个小时的伪代码和尝试后,我提出的最终结果不起作用或不能编译。

I would like to take two vectors A and B, and compare them against each other. 我想取两个向量A和B,并将它们相互比较。 I set up nested struct to read the start and end point of the vector, and the vector struct itself. 我设置了嵌套结构来读取向量的起点和终点,以及向量结构本身。 So I think I may be doing some wrong further below, but I am stuck. 所以我想我可能在下面做了一些错误,但我被困了。

    #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;
}

One problem is with your ReadPoint whose return type is Point , but you're returning an instance of MathVector . 一个问题是您的ReadPoint的返回类型是Point ,但是您正在返回MathVector一个实例。 Also, you read the input into variables which ignore eventually. 此外,您还读取了最终忽略的变量的输入。

You should write ReadPoint as: 你应该把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;
}

Or a little better version: 或者更好的版本:

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

Or, still better is, overload >> operator as: 或者,更好的是,重载>>运算符为:

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;

Now read a good C++ book. 现在阅读一本好的C ++书。 If you're already reading one, then make sure it is really good. 如果您已经阅读了一个,那么请确保它真的很好。 Here is a list of really good C++ books, of all levels: 这是非常好的C ++书籍,各级的列表:

  1. ReadPoint returns letter of type MathVector instead of Point ReadPoint返回MathVector类型的字母而不是Point
  2. You haven't overloaded operator << to tell it how to handle MathVector objects 你没有重载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;

} }

You didn't explain what it is you're trying to do or what errors you got, but this code makes no sense. 你没有解释你正在尝试做什么或者你得到了什么错误,但这段代码毫无意义。 You have three variables, x , y , and z . 你有三个变量, xyz You fill them with values you get from the user. 您可以使用从用户获得的值填充它们。 Then you don't do anything with those variables and return the MathVector created by a default constructor even though you say you're going to return a Point . 然后你不对这些变量做任何事情并返回由默认构造函数创建的MathVector ,即使你说你要返回一个Point That makes very little sense. 这没什么意义。

letterA and letterB are of type MathVector letterAletterB的类型为MathVector

 MathVector letterA;
        MathVector letterB;
        double a_times_b;

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

you should create another method to read Mathvector .. as you are doing with Point . 你应该创建另一个方法来读取Mathvector ..就像你在使用Point

and in method ReadPoint 并在方法ReadPoint

return type must be Point .. If you reading point then do calculation here to create the object of MathVector go tet startpoint and endpoint format. 返回类型必须是Point ..如果你读点,那么在这里做计算来创建MathVector的对象go tet startpointendpoint format。

No match for 'operator=' error means that there's no function for assigning a MathVector to a Point. 'operator ='错误不匹配意味着没有将MathVector分配给Point的功能。 You are calling ReadPoint() which returns a Point and trying to assign the returned value to a variable of type MathVector. 您正在调用ReadPoint(),它返回一个Point并尝试将返回的值分配给MathVector类型的变量。 The compiler can't create a 'convertion' function automatically. 编译器无法自动创建“转换”功能。 You have to provide one yourself. 你必须自己提供一个。 Perhaps what you meant was 也许你的意思是

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

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

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