无法从 C++ 中的类获取值
Cannot get the value from a class in C++?
我刚从Java和Python世界来到C++世界,在尝试从类的公共const
函数中获取值时遇到了一个问题。
我有一个课程如下:
class CMDPoint
{
public:
CMDPoint();
CMDPoint(int nDimensions);
virtual ~CMDPoint();
private:
int m_nDimensions; // the number of dimensions of a point
float* m_coordinate; // the coordinate of a point
public:
const int GetNDimensions() const { return m_nDimensions; }
const float GetCoordinate(int nth) const { return m_coordinate[nth]; }
void SetCoordinate(int nth, float value) { m_coordinate[nth] = value; }
};
最终,我希望将所有clusterPoint
clusterPointArray
写入文件。但是,现在我只是用第一个clusterPoint
对其进行测试(因此,GetCoordinate(0)
(。
ofstream outFile;
outFile.open("C:\data\test.txt", std::ofstream::out | std::ofstream::app);
for (std::vector<CMDPoint> ::iterator it = clusterEntry->clusterPointArray.begin(); it != clusterEntry->clusterPointArray.end(); ++it)
{
outFile << ("%f", (*it).GetCoordinate(0)); // fails
outFile << " ";
}
outFile << "n";
outFile.close();
问题是我只看到文件中的" "
。未写入任何坐标。从const float GetCoordinate(int nth)
获取值时我做错了什么吗?
尝试更改此设置
outFile << ("%f", (*it).GetCoordinate(0)); // fails
对此:
outFile << (*it).GetCoordinate(0); // OK
因为("%f", (*it).GetCoordinate(0))
不表示任何内容,只表示用,
分隔的表达式的枚举。我认为它不会像在 java 中那样被评估为一对对象。
编辑:("%f", (*it).GetCoordinate(0))
实际上计算到最后一个(*it).GetCoordinate(0)
元素(PlasmaHH注释(,因此它仍然应该打印一些东西。但是,如果未打印任何内容,则集合clusterEntry->clusterPointArray
可能为空,并且 for 循环中的代码可能永远不会执行。
希望这有帮助,拉兹万。
outFile << it->GetCoordinate(0);
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 在c++中获取大文件的大小