簡體   English   中英

簡單計數器程序C ++中的錯誤C2228

[英]Error C2228 In a Simple Counter Program 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";
    }
}

出現的錯誤是:

“ dsCh2Exercise.cpp(81):錯誤C2228:'.increment'的左側必須具有類/結構/聯合dsCh2Exercise.cpp(82):錯誤C2228:'.getter'的左側必須具有類/結構/聯合“

提前感謝您的任何輸入! 非常感謝!

counter test(); 聲明一個名為test的函數,該函數不帶任何參數並返回一個counter ,而不是一個名為test包含counter的變量。 將該行更改為:

counter test;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM