gpt4 book ai didi

C++ Unresolved token 问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:13 27 4
gpt4 key购买 nike

我正在为一个类(class)做一个程序,老师给了我们一个我们必须实现的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;
}

这里是错误。

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

最佳答案

您正在声明您从未定义的全局函数 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);

关于C++ Unresolved token 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14506271/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com