C++未解决的令牌问题
C++ Unresolved Token Issue
我正在为一个班级做一个程序,老师给了我们一个我们必须实现的 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 个更改,您的程序就可以准备就绪。 :)
相关文章:
- 如何在C++中使用X509证书模在令牌中查找私钥
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- C++:"("令牌"之前有预期的非限定 id 指向类中成员函数的指针
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 错误:令牌 { '{' 之前应存在非限定 ID
- 允许哪些令牌作为 #include 的参数?
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 它在 { 令牌之前给了我预期的标识符
- (错误:令牌之前'<<'预期的主表达式)
- 为什么 ## aka 令牌粘贴运算符不适用于 C 和 C++ 中的注释?
- 错误:"->"令牌之前的预期初始值设定项
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 在解释器开发期间解析令牌问题
- 令牌问题之前'*'预期的主表达式
- "{"令牌 c++ 继承问题之前的另一个预期类名
- 如何使"strtok function"一次使用多个令牌字符串? 函数指针会解决这个问题吗?
- 错误:"<"令牌之前的预期非限定 ID(模板编译问题)
- 错误:')'令牌强制转换问题之前期望的主表达式
- 返回模板类的模板函数 - GCC 编译问题 - 错误:令牌之前'<'预期非限定 ID
- C++未解决的令牌问题