C++ 使用函数输出结构值

C++ 使用函数输出结构值,c++,function,data-structures,C++,Function,Data Structures,嗨,我刚刚开始尝试结构。我尝试运行一个非常基本的程序,其中结构(x,y)中的两个点由函数输出。我知道这是很基本的,但我一整天都在尝试,就是想不出来。任何帮助都将不胜感激 using namespace std; void printPoint(const pointType& point); struct pointType { int x; int y; } int _tmain(int argc, _TCHAR* argv[]) { struct p

嗨,我刚刚开始尝试结构。我尝试运行一个非常基本的程序,其中结构(x,y)中的两个点由函数输出。我知道这是很基本的,但我一整天都在尝试,就是想不出来。任何帮助都将不胜感激

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

    //      
}
这可能有用

 #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;
    //      
}
#包括
使用名称空间std;
结构点类型
{
int x;
int-y;
};
无效打印点(常量点类型和点);
int main(int argc,字符**argv)
{
结构点类型pos1;
pos1.x=10;
pos1.y=15;
printPoint(pos1);
//系统(“暂停”);
返回0;
}
无效打印点(常量点类型和点)
{

许多可能性之一是

void printPoint(const pointType& point){
  std::cout << "x:" << point.x << ", y:" << point.y << std::endl;
}
void打印点(常量点类型和点){

std::cout您应该在函数声明之前定义结构,或者函数声明应该使用一个详细的名称,即带有关键字struct的结构名称

struct pointType {
int x;
int y;
};

void printPoint(const pointType& point); 

否则,编译器将不知道函数声明中的名称pointType是什么意思

结构定义应以分号结尾

struct pointType {
int x;
int y;
};
在这份声明中

struct pointType pos1;
不需要指定关键字struct,您可以编写更简单的代码

pointType pos1;
您还可以通过以下方式初始化对象

struct pointType pos1 =  { 10, 15 };
void printPoint(const pointType& point)
{
   std::cout << "x = " << point.x << ", y = " << point,y << std::endl;
}
函数调用应该有一个参数,因为它被声明为有一个参数

printPoint();

函数本身可以如下所示

struct pointType pos1 =  { 10, 15 };
void printPoint(const pointType& point)
{
   std::cout << "x = " << point.x << ", y = " << point,y << std::endl;
}
void打印点(常量点类型和点)
{

std::cout您如何从函数中输出一个
int
?接受它,并修改它,使其与您的
struct
一起工作。您的意思是
printf(“%d”,point->x”)在C++中,习惯性C++答案是建立一个<代码>操作符,为什么建议代码< > PrtufF>代码>?JoeDF我没有反对C。但是如果OP使用C++并尝试学习C++,习惯性C++答案最好。为什么朋友?数据成员是公共的。
printPoint( pos1 );
void printPoint(const pointType& point)
{
   std::cout << "x = " << point.x << ", y = " << point,y << std::endl;
}