2011-11-25 41 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();