Reputation: 67
I am doing a program for a class and the teacher gave us a Cpp file that we had to implement. All was written by him except Main but I am getting a weird error. Any help would be great. Here is my code.
// **************************************************************************
//
// Counter.cpp
//
// Defines and tests class CounterType, which is used to count things.
// CounterType contains both a default constructor and a constructor that
// sets the count to a specified value, plus methods to increment, decrement,
// return, and output the count. The count is always nonnegative.
//
// **************************************************************************
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class CounterType
{
public:
CounterType();
//Initializes the count to 0.
CounterType(int initCount);
//Precondition: initCount holds the initial value for the count.
//Postcondition:
// If initCount > 0,initializes the count to initCount.
// If initCount <= 0,initializes the count to 0.
void increment();
//Postcondition:
// The count is one more than it was.
void decrement();
//Postcondition:
// If the count was positive, it is now one less than it was.
// If the count was 0, it is still 0
int getCount();
void output(ostream& outStream);
//Precondition: outStream is ready to write to
//Postcondition: count has been written to outStream
private:
int count;
};
void increment();
void decrement();
int getCount();
void output(ostream& outStream);
int main()
{
CounterType Test;
increment();
decrement();
getCount();
}
CounterType::CounterType()
{
count = 0;
}
CounterType::CounterType(int initCount)
{
if (initCount >= 0)
count = initCount;
else
count = 0;
}
void CounterType::increment()
{
count++;
}
void CounterType::decrement()
{
if (count > 0)
count--;
}
int CounterType::getCount()
{
return count;
}
void CounterType::output(ostream& outStream)
{
outStream << count;
}
And here is the error.
Error 1 error LNK2028: unresolved token (0A000330) "void __cdecl decrement(void)" (?decrement@@$$FYAXXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)J:\MCM 10.05\MCM 10.05\MCM10.obj MCM 10.05
Upvotes: 0
Views: 526
Reputation: 126522
You are declaring global functions increment()
, decrement()
, and getCount()
which you never define. You get a linking error because you invoke them in main()
and the linker cannot find their definitions.
You probably meant to invoke the member functions of the Counter
object, like this:
int main()
{
CounterType Test;
Test.increment();
Test.decrement();
Test.getCount();
}
If that's the case, you should remove the declarations of the global functions:
// THESE DECLARATIONS BEFORE main() SHOULD NOT BE THERE! JUST REMOVE THEM
// void increment();
// void decrement();
// int getCount();
// void output(ostream& outStream);
Upvotes: 5
Reputation: 2171
You did 2 mistakes :
1) By looking your code it seems, you additionally declare following functions
void increment();
void decrement();
int getCount();
void output(ostream& outStream);
You already provided their declarations in class, So now no need to declare again.
2) Inside main you are calling functions in this manner,
increment();
decrement();
getCount();
which probably you not wants to do, because calling in this way will invoke global functions. The correct way to call class functions is by class object
Test.increment();
Test.decrement();
Test.getCount();
Just by correcting this 2 changes, your program is ready and good to go. :)
Upvotes: 1
Reputation: 500873
You declare a global function called decrement()
and don't define it.
There exists CounterType::decrement()
, but that's a different function.
The same goes for increment()
and getCount()
.
Upvotes: 1