C++未解决的令牌问题

C++ Unresolved Token Issue

本文关键字:问题 令牌 未解决 C++      更新时间:2023-10-16

我正在为一个班级做一个程序,老师给了我们一个我们必须实现的 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:未解析的令牌 (0A000330) "void __cdecl decrement(void)" (?decrement@@$$FYAXXZ) 函数"int __cdecl main(void)" (?main@@$$HYAHXZ)J:\MCM 10.05\MCM 10.05\MCM10.obj MCM 10.05

你声明的全局函数increment()decrement()getCount()你从未定义过。您会收到链接错误,因为您在main()中调用它们,并且链接器找不到它们的定义。

您可能打算调用 Counter 对象的成员函数,如下所示:

int main()
{
    CounterType Test;
    Test.increment();
    Test.decrement();
    Test.getCount();
}

如果是这种情况,您应该删除全局函数的声明:

// THESE DECLARATIONS BEFORE main() SHOULD NOT BE THERE! JUST REMOVE THEM
// void increment();
// void decrement();
// int getCount();
// void output(ostream& outStream);

你声明了一个名为 decrement() 的全局函数,但没有定义它。

存在CounterType::decrement(),但这是一个不同的功能。

increment()getCount()也是如此.

你犯了 2 个错误:

1)通过查看您的代码,您似乎还声明了以下函数

 void increment();
 void decrement();
 int getCount();
 void output(ostream& outStream);

您已经在课堂上提供了他们的声明,所以现在无需再次声明。

2)在 main 中,您以这种方式调用函数,

increment();
decrement();
getCount();

您可能不想这样做,因为以这种方式调用将调用全局函数。调用类函数的正确方法是按类对象

Test.increment();
Test.decrement();
Test.getCount();

只需更正这 2 个更改,您的程序就可以准备就绪。 :)