我正在为一个班级做一个程序,老师给了我们一个我们必须实现的 Cpp 文件。除 Main 外,所有内容都是由他编写的,但我遇到了一个奇怪的错误。任何帮助都会很棒。这是我的代码。
// **************************************************************************
//
// 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;
}
这是错误。
错误 1 错误 LNK2028:在函数“int __cdecl main(void)”(?main@@$$HYAHXZ)J:\ MCM 10.05\MCM 10.05\MCM10.obj MCM 10.05