alberto roberto
alberto roberto

Reputation: 29

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

Upvotes: 0

Views: 168

Answers (5)

frozenkoi
frozenkoi

Reputation: 3248

No match for 'operator=' error means that there's no function for assigning a MathVector to a Point. You are calling ReadPoint() which returns a Point and trying to assign the returned value to a variable of type 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();

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361782

One problem is with your ReadPoint whose return type is Point, but you're returning an instance of MathVector. Also, you read the input into variables which ignore eventually.

You should write ReadPoint as:

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. 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:

Upvotes: 2

David Schwartz
David Schwartz

Reputation: 182885

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. 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. That makes very little sense.

Upvotes: 0

Niranjan Singh
Niranjan Singh

Reputation: 18260

letterA and letterB are of type 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.

and in method 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.

Upvotes: 0

adrianton3
adrianton3

Reputation: 2358

  1. ReadPoint returns letter of type MathVector instead of Point
  2. You haven't overloaded operator << to tell it how to handle MathVector objects

Upvotes: 0

Related Questions