0 c++
第一次在这里,如果我不完全遵循协议,请原谅我.我会根据需要进行调整.我正在尝试制作一个通过计数器递增(或递减)的简单程序.计数器的功能是通过一个类,我正在尝试使用main来测试功能.我很容易错过一些非常简单的东西,就像我一直如此,但我无法弄清楚所以我想我会问这里,因为我经常来这里寻找帮助很容易.我已经尝试过筛选答案,到目前为止没有任何帮助.这是代码:
#include <iostream>
using namespace std;
class counter
{
public:
counter();
counter(int begin, int maximum);
void increment();
void decrement();
int getter();
private:
int count;
int max;
};
// Default constructor.
counter::counter()
{
count = 0;
max = 17;
}
// Constructor that allows you to put in a starting point for the counter
// and a maximum value for the counter.
counter::counter(int begin, int maximum)
{
max = maximum;
if (begin > maximum)
{
cout << "You input an invalid value to begin. Set to default.";
count = 0;
}
else
{
count = begin;
}
}
// Increments counter by one. If counter would exceed max, then goes to 0.
void counter::increment()
{
if (count == max)
{
count = 0;
}
else
{
count++;
}
}
// Decrements counter by one. If counter we go below 0, then goes to max.
void counter::decrement()
{
if (count == 0)
{
count = max;
}
else
{
count--;
}
}
// Getter for counter value.
int counter::getter()
{
return count;
}
int main()
{
counter test();
for (int i = 0; i < 20; i++)
{
test.increment();
cout << test.getter() << "\n";
}
}
Run Code Online (Sandbox Code Playgroud)
出现的错误是:
"dsCh2Exercise.cpp(81):错误C2228:'.increment'的左边必须有class/struct/union dsCh2Exercise.cpp(82):错误C2228:'.getter'的左边必须有class/struct/union"
提前感谢任何和所有输入!非常感谢!
counter test();声明一个名为a的函数test,该函数不带参数并返回a counter,而不是名为test包含a 的变量counter.将该行更改为:
counter test;
Run Code Online (Sandbox Code Playgroud)