0
第一次,所以原谅我,如果我不能完全以下协议。我会根据需要进行调整。我试图做一个简单的程序,通过计数器递增(或递减)。计数器的功能是通过一个类,我试图用main来测试功能。我可以很容易地失去了一些东西超级简单,因为总是跟我的情况,但我无法弄清楚,所以我想我会在这里问,因为我已经来到这里非常往往容易寻求帮助。我试过筛选答案,迄今为止没有任何帮助。下面的代码:错误C2228在一个简单的计数器程序C++这里
#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";
}
}
那的显现是错误:
“dsCh2Exercise.cpp(81):错误C2228:左 '.increment' 必须 类/结构/联合dsCh2Exercise。 CPP(82):错误C2228:左 '.getter' 必须有类/结构/联合”
由于时间提前的任何和所有输入!非常感谢!
哇哦。毕竟,这似乎是愚蠢的。我一直在尝试为求职面试准备多种语言,并且练习基本的东西来习惯语法。 不管怎么说,谢谢你这么多,我真正体会到它。 –
这是在C++中一个常见的错误,因为在构造函数采用参数,你可以使用这个语法。这只是一个空的参数列表模糊不清,然后它被解析为一个声明。 – Barmar
很高兴知道。我会在我容易忽略的事情中写下这些内容,我保持得心应手。 –