简体   繁体   中英

using a function to output struct values

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)
{

    //      
}

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

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM