2011-11-25 43 views
0

我使用嵌套的結構/結構,經過幾個小時的僞代碼和嘗試,我提出的最終結果不起作用或不能編譯。C++結構不編譯...未正確初始化?不正確使用它們?

我想採取兩個向量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; 
} 
+0

發佈錯誤。另外,「不工作」和「不編譯」是兩回事。 – Nawaz

+0

事實上'ReadPoint()'不會更新'letter'一個錯字? –

+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++書籍列表,各級:

+0

感謝Nawaz ....現在我只學習C++結構,在我的高中課程之前跳過:)如果我在單一課程或大學課程中進行一些編程,我肯定會看看 –

+0

感謝您的幫助!我對我的方式... LETTERA = ReadPoint(); letterB = ReadPoint(); 時說: 「在 'letterB = ReadPoint)(' 敵不過 '運算符='」 這是什麼意思 –

+0

你?試圖設置一個等於一個點的向量,但是你從未指定過應該做什麼電腦不知道。 –

0
  1. ReadPoint返回類型MathVector,而不是點的信
  2. 您還沒有重載操作< <告訴它如何處理MathVector對象
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; 

}

你沒有解釋什麼是你正在試圖做的還是你有什麼錯誤,但是這個代碼使得沒有感。您有三個變量,x,yz。你用你從用戶那裏得到的值填充它們。然後你不會對這些變量做任何事情,並且返回由默認構造函數創建的MathVector,即使你說你要返回Point。這沒什麼意義。

+0

我創建了一個向量,點,應該處理三個​​維度,x y z。 然後創建一個結構MathVector,以便向量的START和向量的END可以使用struct Point。這些MathVectors中的兩個創建爲'A'和'B',以便可以將兩個獨立向量放入計算中。 –

+0

好的,所以你需要讀* 4 *點並將它們加載到兩個向量的'start'和'end'位置。你從來沒有寫任何代碼來做到這一點。 –

0

letterAletterB的類型MathVector

MathVector letterA; 
     MathVector letterB; 
     double a_times_b; 

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

你應該建立另一種方法來讀取Mathvector ..作爲你與Point做。

和方法ReadPoint

返回類型必須是點..如果你閱讀點然後在這裏做計算創建對象MathVector go tet startpointendpoint格式。

0

'operator ='錯誤的匹配意味着沒有將MathVector分配給Point的函數。您正在調用ReadPoint(),它將返回一個Point並嘗試將返回的值分配給MathVector類型的變量。編譯器不能自動創建「轉換」功能。你必須自己提供一個。也許你的意思是

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