0

Hi I have just begun experimenting with structures. I'm try to run a very basic programme in which two points in a struct (x,y) are outputted by a function. I know it's very basic but Ive been trying all day and just can't figure it out. Any help would be greatly appreciated!

using namespace std;

void printPoint(const pointType& point); 

struct pointType 
{
    int x;
    int y;
}

int _tmain(int argc, _TCHAR* argv[])
{
    struct pointType pos1;
    pos1.x = 10;
    pos1.y = 15;

    printPoint();


    system("pause");
    return 0;
}

void printPoint(const pointType& point)
{

    //      
}
3
  • 3
    How would you output, say, a single int from a function? Take that, and modify it so it works with your struct. Commented Mar 14, 2014 at 13:57
  • Did you mean printf("%d", point->x) inside your function? Commented Mar 14, 2014 at 13:59
  • The idiomatic C++ answer would be to build an operator<< for iostreams, and use it with std::cout, a simpler way is to access the members directly. Commented Mar 14, 2014 at 14:04

3 Answers 3

1

This might work

 #include <iostream>

using namespace std;

struct pointType
{
    int x;
    int y;
};

void printPoint(const pointType& point); 


int main(int argc, char** argv)
{
    struct pointType pos1;
    pos1.x = 10;
    pos1.y = 15;

    printPoint(pos1);


    //system("pause");
    return 0;
}

void printPoint(const pointType& point)
{
    cout << point.x << '\t' << point.y << endl;
    //      
}
Sign up to request clarification or add additional context in comments.

2 Comments

Why suggest printf for a C++ question?
@JoeDF I have nothing against C. But if the OP is using C++ and trying to learn C++, idiomatic C++ answers are best.
1

One of many possibilities is

void printPoint(const pointType& point){
  std::cout << "x:" << point.x << ", y:" << point.y << std::endl;
}

Overload operator<<

However in case of more complicated logic of the output operation you can define operator<<( std::ostream& oot, pointType const& p) in your class. This is useful when you want do something additional when writing to output stream is made, or when what will be printed is not simply built-in type, so you cannot write std::cout << point.x directly. Maybe you want also to use different locale or facet for printing variables of particular type so you would also imbue a stream inside overloaded operator.

struct pointType {
  int x;
  int y;
  friend std::ostream& operator<<( std::ostream &out, pointType const& p);
    ^
  // needed when access to private representation is required,
  // here it is not the case, friend specifier not required
}

std::ostream& operator<<( std::ostream &out, pointType const& p) {
  //.. do something maybe
  out << "x:" << point.x << ", y:" << point.y << std::endl;
  //.. maybe something more
  return out;
}

So now you can use it simply in the usual way output stream is used:

int main() {
  pointType p;
  std::cout << p;
  return 0;
}

1 Comment

Why friend though? The data members are public.
0

Either you should define the structure before the function declaration or the function declaration should use an elaborated name that is the name of the structure with keyword struct

struct pointType {
int x;
int y;
};

void printPoint(const pointType& point); 

or

void printPoint(const struct pointType& point); 

struct pointType {
int x;
int y;
};

Otherwise the compiler will not know what name pointType means in the function declaration.

The structure definition shall be ended with a semicolon

struct pointType {
int x;
int y;
};

In this statement

struct pointType pos1;

there is no need to specify keyword struct, You could write simpler

pointType pos1;

Also you could initialize the object the following way

struct pointType pos1 =  { 10, 15 };

The function call shall have an argument because it was declared as having a parameter. So instead of

printPoint();

write

printPoint( pos1 );

The function itself could look the following way

void printPoint(const pointType& point)
{
   std::cout << "x = " << point.x << ", y = " << point,y << std::endl;
}

Do not forget to include header <iostream>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.