-
December 7th, 2018, 06:01 AM
#1
[RESOLVED] Burnt out. Not sure what I'm doing wrong
Code:
Counter
{
int counter;
int counterID;
int nCounters;
Counter(int preset);
void increment();
void decrement();
int getValue();
int getCounterID();
};
int Counter::nCounters = 0;
Counter::Counter(int value)
{
counter = value;
nCounters = nCounters + 1;
counterID = nCounters;
}
void Counter::increment()
{
counter++;
}
void Counter::decrement()
{
counter--;
}
int Counter::getValue()
{
return counter;
}
int Counter::getCounterID()
{
return counterID;
}
Last edited by 2kaud; December 7th, 2018 at 09:12 AM.
Reason: Fixed code tags
-
December 7th, 2018, 06:27 AM
#2
Re: Burnt out. Not sure what I'm doing wrong
It looks like you forgot to say 'struct' or 'class'.
If you choose 'class', then you need to make your member functions public.
-
December 7th, 2018, 09:14 AM
#3
Re: Burnt out. Not sure what I'm doing wrong
Code:
int Counter::nCounters = 0;
... and ncounters static!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
December 7th, 2018, 10:58 AM
#4
Re: Burnt out. Not sure what I'm doing wrong
Thank you! I figured it out!
Code:
int Counter::nCounters = 0;
Counter::Counter(int i)
{
counter = i;
nCounters++;
counterID = nCounters;
}
void Counter::increment()
{
counter++;
}
int Counter::getValue()
{
return counter;
}
void Counter::decrement()
{
counter--;
}
int Counter::getCounterID()
{
return counterID;
}
Last edited by 2kaud; December 7th, 2018 at 11:18 AM.
Reason: Fixed code tags
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|