如何解决C++中类的组合
这是一个类组成的例子。但是我的代码没有显示正确的答案。这个程序一定要计算线段两端坐标中线段的长度! 我不知道在主函数中做什么。这段代码由两个类组成。
#include <cmath>
#include <iostream>
using namespace std;
class point {
private:
float x;
float y;
public:
void setpoint(float abscissa,float ordinate) {
x = abscissa;
y = ordinate;
}
float getx() { return x; }
float gety() { return y; }
};
class Linesegment {
public:
float length() {
result = sqrt(pow(a.getx() - b.getx(),2) + pow(a.gety() - b.gety(),2));
return result;
}
void displayMessage() { cout << result; }
private:
float result;
point a;
point b;
};
int main() {
point a;
float q,s;
cout << "Enter two numbers for first point:\n";
cin >> q >> s;
a.setpoint(q,s);
point b;
float e,r;
cout << "Enter two numbers for second point:\n";
cin >> e >> r;
a.getx();
a.gety();
Linesegment pt;
pt.length();
pt.displayMessage();
return 0;
}
解决方法
const int PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY = 0x20007;
const long PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON = 0x100000000000L;
[DllImport("kernel32.dll",SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool InitializeProcThreadAttributeList(
IntPtr lpAttributeList,int dwAttributeCount,int dwFlags,ref IntPtr lpSize);
[DllImport("kernel32.dll",SetLastError=true)]
public static extern bool DeleteProcThreadAttributeList(IntPtr lpAttributeList);
[DllImport("kernel32.dll",SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UpdateProcThreadAttribute(
IntPtr lpAttributeList,uint dwFlags,IntPtr Attribute,IntPtr lpValue,IntPtr cbSize,IntPtr lpPreviousValue,IntPtr lpReturnSize);
...
IntPtr attrListSize = 0;
InitializeProcThreadAttributeList(
IntPtr.Zero,1,ref attrListSize);
IntPtr attrList = Marshal.AllocHGlobal(attrListSize);
InitializeProcThreadAttributeList(
attrList,ref attrListSize);
IntPtr lpValue = Marshal.AllocHGlobal(sizeof(long));
Marshal.WriteInt64(lpValue,PROCESS_CREATION_MITIGATION_POLICY_BLOCK_NON_MICROSOFT_BINARIES_ALWAYS_ON);
UpdateProcThreadAttribute(
attrList,(IntPtr)PROC_THREAD_ATTRIBUTE_MITIGATION_POLICY,lpValue,sizeof(long),IntPtr.Zero,IntPtr.Zero
);
// use attrList as needed,ie in STARTUPINFOEX.lpAttributeList ...
DeleteProcThreadAttributeList(attrList);
Marshal.FreeHGlobal(lpValue);
Marshal.FreeHGlobal(attrList);
函数的 a
和 b
局部变量与 main()
类的 a
和 b
成员无关。您应该给 LineSegment
一个构造函数,通过它您可以将端点传递给它,或者至少提供一个或多个方法,可以在事后设置端点。在设置端点之前,您不得尝试计算段长度。
成员 a
和成员 b
都没有为 pt
初始化。您需要对它们进行初始化,或者使用 point
的默认构造函数对其进行初始化,该构造函数碰巧不进行任何初始化,从而导致未定义的行为。
你可以例如将 point
传递给 LineSegment
的构造函数以解决此问题:
class LineSegment {
public:
LineSegment(const point& p1,const point& p2)
: a(p1),b(p2)
{}
...
};
...
LineSegment pt {a,b};
...
注意:我建议为成员变量添加前缀。 m_
是一种选择(即您将使用 m_a
和 m_b
作为成员变量名称)。这样你就可以避免这样的混淆,也可以避免变量的阴影,假设这是唯一使用这个前缀的变量。
编辑:您也永远不会在 setpoint
的 b
上调用 main
;您需要在将点传递给上述代码段中的构造函数之前执行此操作。
这是你的代码,修改后可以工作:
#include <cmath>
#include <iostream>
// using namespace std; // CHANGED: Bad practice
class point {
private:
float x = 0.0f; // CHANGED: Add default member initialization
float y = 0.0f;
public:
point() = default; // CHANGED: Add default constructor
point(int abscissa,int ordinate) : x(abscissa),y(ordinate) {} // ADDED
void setpoint(float abscissa,float ordinate) {
x = abscissa;
y = ordinate;
}
float getx() const { return x; } // CHANGED: Mark getters as const
float gety() const { return y; }
};
class LineSegment {
public:
// CHANGED: Add constructor so you can actually build a LineSegment
LineSegment(point one,point two) : a(one),b(two) {}
// CHANGED: Made a one-liner
float length() const {
return sqrt(pow(a.getx() - b.getx(),2) + pow(a.gety() - b.gety(),2));
}
// void displayMessage() const { std::cout << result; } // CHANGED: Not
// needed
private:
// float result; // CHANGED: Does not need to stored
point a;
point b;
};
int main() {
float q,s;
std::cout << "Enter two numbers for first point:\n";
std::cin >> q >> s;
point a(q,s); // Can now directly initialize
float e,r;
std::cout << "Enter two numbers for second point:\n";
std::cin >> e >> r;
point b(e,r); // CHANGED: Actually put values into b
// a.getx(); // CHANGED: These do nothing
// a.gety();
LineSegment pt(a,b); // CHANGED: Actually put data into pt
std::cout << "\nLine Length: " << pt.length() << '\n'; // CHANGED: Make use
// of functions now
// available
return 0;
}
您最大的问题是没有正确初始化您的对象。 point b
从未被赋予值 e
和 r
,LineSegment pt
从未被赋予任何 point
。
进行这些小的更改,您的代码就会按预期工作。仅使用 (0,0) 和 (1,1) 的简单示例即可提供 1.41421
的输出,即 root-2,这是正确的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。