C++代码段执行
C++ Code Snippet Execution
我正在写过去的一篇试卷,我想知道是否有人能解释这个问题的解决方案:考虑到这个(不正确的)代码片段是一个头文件。
#include <iostream>
using namespace std;
class Weight
{
public:
Weight(const int = 0, const int = 0);
Weight(const int = 0);
int totalPounds();
Weight operator+(const Weight);
Weight operator++();
Weight operator++(int);
private:
int stones;
int pounds
};
void operator<<(ostream& os, const Weight&);
在主方法中执行,并假设.cpp类与所述头文件的实现一起存在。
Weight a(12);
const Weight b(15, 3);
const int FIXED_WEIGHT = b.totalPounds();
Weight combined = a + b;
++a;
b++
combined = 5 + a;
a = b + 1;
cout << a << b;
哪一行会导致头文件出错,需要对头文件进行哪些修改?
我真的很困惑,我们在课堂上几乎没有涵盖默认参数。。。我试着删除它们,这样代码就可以工作了,但我认为这不是解决方案。此外,代码行const int = 0
的含义是什么,以及我将如何实现基于它的东西。这不会导致定义不明确的构造函数吗?
假设pounds
和b++
之后缺少的;
是打字错误,我看到的错误是:
- 构造函数不明确。你只需要第一个
b
是const,因此调用totalPounds
失败,因为它不是const方法b
是const,所以后增量失败,因为它不是const方法5 + a
失败,因为没有匹配的+
运算符可供使用b
是const,因此b + 1
失败,因为+
不是const方法operator<<
的void
返回值导致cout
语句失败operator<<(ostream& os, const Weight&)
不是朋友,因此无法实际打印Weight
的内部值
相关文章:
- 需要将此代码更改为执行代码
- 当前不会命中断点。没有调试器目标代码类型的可执行代码与此文件关联
- 单步执行代码时重复上一行
- 如何使用介子在C++中执行代码覆盖?
- Visual Studio 2017,C++,在单步执行代码时指向错误的行
- 在 R 中执行C++代码
- 通过 dll 注入在主线程中执行代码
- 无法在 c++ 中循环后执行代码
- 执行C 代码时快速频繁的文件访问
- 执行 C++ 代码后出错
- 一个人如何从代表函数的字符串中执行运行时执行C 代码
- 是否可以在程序崩溃后执行代码?
- 第一次在 Linux 上执行 c++ 代码的时间非常慢
- 计算 JSON 中的条目数并相应地执行代码
- 将在 CATCH 块之后执行代码
- 分析执行C++代码的每一行所花费的确切时间
- 我在执行代码时不断得到"Bus Error"?
- c while()..执行代码行的条件
- 从并行线程在主 Maya 线程上执行代码
- 在调用GNUPLOT之后,如何继续执行C 代码