[英]Declaration in header of function in struct in C++ (easy)
我有3个文件,分别是作业需要的1个main.cpp,1 Point.cpp和1 Point.h。 问题:问两个整数,先保存在struct-object中,然后输出最高的数字:main.cpp
#include <iostream>
#include "Uebung8_1.h"
using std::cout;
using std::cin;
int main(){
int z;
int r;
cout << "Enter x: " << "\n";
cin >> z;
cout << "Enter y: " << "\n";
cin >> r;
Point point;
point.x = z;
point.y = r;
if (point.simsalabim(point.x, point.y) != true){
int h = point.y;
point.y = point.x;
point.x = h;
}
cout << "\n" << point.x <<"\n" << point.y;
return 0;
}
Point.cpp
struct Point{
int x;
int y;
bool simsalabim(int x, int y){
if (x >= y){
return true;
}
else{
return false;
}
}
};
Point.h
struct Point{ int x; int y; bool simsalabim(int x, int y);};
该代码可以正常工作,而无需声明(和使用)simsalabim,该声明应确定x是否小于y。 我认为问题出在标题中,但是请记住,我应该使用程序的这种结构,并且不能完全决定它的外观。 它看起来应该像这样。 你能帮我做这份工作吗?
提前Thx
我认为Point.cpp应该看起来更像
#include "Point.h"
bool Point::simsalabim(int x, int y){
return x >= y;
}
您应该将simsalabim
更改为:
struct Point{
int x;
int y;
bool simsalabim(){
return (x >= y);
}
};
并更改其调用方式:
if (point.simsalabim() != true){
int h = point.y;
point.y = point.x;
point.x = h;
}
在Point.cpp中,不要再次包含类声明。 仅具有成员函数定义。
Point.h:
struct Point{
int x;
int y;
bool simsalabim();
};
Point.cc:
#include "Point.h"
bool Point::simsalabim(){
return (x >= y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.