C++ "undefined reference to..."
C++ "undefined reference to..."
我不知道为什么我的代码出错了,我需要一些帮助。
首先,代码:Timer.h:
#include [...]
class Timer {
public:
[...]
Timer operator+(double);
[...]
private:
[...]
void correctthedate(int day, int month, int year);
[...]
};
Timer.cc:
#include "Timer.h"
using namespace std;
[...]
void correctthedate(int day, int month, int year) {
[...]
}
[...]
Timer Timer::operator+(double plush) {
[...]
correctthedate(curday, curmonth, curyear);
return *this;
}
当我尝试编译时,我得到错误:
Timer.o: In function `Timer::operator+(double)':
Timer.cc:(.text+0x1ad3): undefined reference to `Timer::correctthedate(int, int, int)'
有正确方向的指示吗?谢谢!
下一行:
void correctthedate(int day, int month, int year) {
应该读
void Timer::correctthedate(int day, int month, int year) {
否则你只是定义了一个不相关的函数correctthedate()
Write
void Timer::correctthedate(int day, int month, int year) {
您的correctthedate
定义是一个自由函数,尽管没有原型。您必须使用Timer::
替换为
void correctthedate(int day, int month, int year) {
与这个:Timer::correctthedate(int day, int month, int year) {
在您的版本中,correctthedate
只是一个普通的函数,它恰好与Time
的一个方法具有相同的名称。Time::correctthedate
是一个完全不同的函数(方法),它没有定义,所以链接器抱怨它找不到它。
你的头声明了一个Timer::operator+
和一个Timer::correctthedate
函数。
您的cpp定义了Timer::operator+
和::correcttehdate
函数。
链接器找不到Timer::correctthedate
。
答案是将void correctthedate(int...
改为void Timer::correctthedate(int...
相关文章:
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- Visual Studio Code "undefined reference to `WinMain@16'"
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- 不断"Attempting to reference a deleted function"
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- Libcurl c++ "undefined reference to" (Windows/MinGW/g++)
- 尝试使用 extern "C" 调用 C 中的C++方法,得到"undefined reference to"对象的链接器错误
- 与映射和unordered_map相关的编译错误:"attempting to reference a deleted function"
- 将 QMutex 添加到类后的"Attempting to reference a deleted function"
- 多级继承错误 C2280:"atttempting to reference a deleted function"
- static_cast int to reference to int?
- C++:具有 const int 的类的 vector.erase 实例给出"attempting to reference a deleted function"错误 C2280
- "attempting to reference a deleted function"函数参数
- 错误 "Attempting to reference a deleted function" - 传回 unique_ptr 的值时
- 尝试使用 std::bind 重新分配 std::function 并得到错误"attempting to reference a deleted function"
- 我怎么知道"C2280 - attempting to reference a deleted function"发生在哪里?
- "Attempting to reference a deleted function"