Hello everyone;
I've been struggling with trying to figure out how to get this struct program to work and I'm at a total loss on this. Basically I need to create two structs POINT which will have an X and Y values, and a LINE struct which will include 2 POINT structures as its data members. The program also has a function called getData and takes input from the user, and another function called printData which will be the endpoints of a line. However, when trying to test to see if I even have my code correct, I keep getting "error C2597: illegal reference to non-static member 'Point::m_Xcoord'", and I'm not sure where to go to get past this. I've been working on this for several days now, and can't seem to get past this error. The code I'm using is as follows:

#include "stdafx.h"
using namespace std;

//Create structs POINT and LINE
struct Point
{
    float m_Xcoord;
    float m_Ycoord;

};  // closing brace for struct

struct Line
{
    Point m_Left;
    Point m_Right;
    static void getData();
    static void printData();
};  // closing brace for struct


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Welcome to the Struct Program" << endl;

    Line aLine;
    aLine.getData();
    aLine.printData();
    return 0;
}

//Functions
void getData(Line aLine)
{
    //int x,y;

    cout << "/nYou will need to enter in 2 for this program." << endl;
    cout << "/nPlease enter in your 2 coordinates: ";
    cin >> Point::m_Xcoord >> Point::m_Ycoord;
    //cout << "/n/nPlease enter in your 2 Y coordinates: ";
    //cin >> c >> d;

} //closing bracket for function

void printData(Line aLine)
{
    cout << "/nYour line starts with: " << aLine.m_Left << " and ends with: " << aLine.m_Right << endl;

    cout << "/nThank you for trying this program." << endl;
    cout << "/nPress any key to continue.......";
} // closing brace for function

`
I'm hoping that I'm close to the ballpark area here, but have a feeling I'm not too clear on structs.... Any help would be very appreciated with this one.
Thanks,
dax

Recommended Answers

All 3 Replies

getData() needs to collect 4 pieces of data, two for each Point in the line, m_Left.m_Xcoord, m_Left.M_Ycoord and similarly for m_Right.

In order to do this:
cout << aLine.m_Left;
you need to overload the << operator for the Pointer class.

Ok... I'm not sure I understand how or where to overload the >> operator. I've broken out the CIN statements in the getData function, but now getting another error: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) This error is from the cin >> aLine.m_Left; within the getData function. Can you show me an example of how to overload the >> operator? Here's the changes I've made:

#include "stdafx.h"
using namespace std;

//Create structs POINT and LINE
struct Point
{
    float m_Xcoord;
    float m_Ycoord;

};  // closing brace for struct

struct Line
{
    Point m_Left;
    Point m_Right;
    static void getData();
    static void printData();
};  // closing brace for struct


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Welcome to the Struct Program" << endl;

    Line aLine;
    aLine.getData();
    aLine.printData();
    return 0;
}

//Functions
void getData(Line aLine, Point p1, Point p2)
{
    //Point p1, p2;

    cout << "/nYou will need to enter in 2 X and 2 Y coordinates for this program." << endl;
    cout << "/nPlease enter in your first X coordinate: ";
    cin >> p1.m_Xcoord; 
    cout << "/nPlease enter in your second X coordinate: ";   
    cin >> aLine.m_Left;
    cout << "/n/nPlease enter in your first Y coordinate: ";
    cin >> p2.m_Ycoord;
    cout << "/nPlease enter in your second Y coordinate: ";
    cin >> aLine.m_Right;

} //closing bracket for function

void printData(Line aLine)
{
    cout << "/nYour line starts with: " << aLine.m_Left << " and ends with: " << aLine.m_Right << endl;

    cout << "/nThank you for trying this program." << endl;
    cout << "/nPress any key to continue.......";
} // closing brace for function

Thanks again for any and all help with this.....
dax

cin >> Point::m_Xcoord >> Point::m_Ycoord;

I didn't take the effort to understand what you are trying to do but there is definitely an error at line 38.
You cannot access structure member variables using :: operator unless they are static. You need to create an instance of structure to access it. Or you have to make the members static.

1.

    struct Point
    {
        float m_Xcoord;
        float m_Ycoord;

    };

    int main ()
    {
        Point instance; //create an instance
        cin >> instance.m_Xcoord >> instance.m_Ycoord; //access using instance
    }

OR

2.

    struct Point
    {
        static float m_Xcoord; //make it static
        static float m_Ycoord; //make it static

    };

    int main()
    {
        cin >> Point::m_Xcoord >> Point::m_Ycoord;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.