簡體   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