c++ - 对类构造函数 h 文件的 undefined reference

标签 c++ c++11 compiler-errors header-files undefined-reference

这个问题在这里已经有了答案:





What is an undefined reference/unresolved external symbol error and how do I fix it?

(37 个回答)


1年前关闭。




我有一个程序的多个类,并且我已经修复了所有错误,但是当我尝试编译时,它现在显示对 `event::event(std::__cxx11::basic_string, std::__cxx11::basic_string, int, int)'
我是 cpp 的新手,无法理解这里发生的事情。其他类也报告了相同的错误。会不会是 h 文件有问题。我在下面粘贴一个,有人可以告诉我如何解决这个问题。

#ifndef EVENT_H
#define EVENT_H

#include <string>
#include <iostream>
#include <iomanip>

class event
{
protected:
    std::string location, uniqueFeature;
    double uniFeaFactorT, uniFeaFactorF;

public:
    event(std::string location = "", std::string uniqueFeature = "", int uniFeaFactorT = 0, int uniFeaFactorF = 0);
    double getUFF(bool f);
    std::string getLoc();
    std::string getUF();
};

double event::getUFF(bool f){ return (f) ? uniFeaFactorT : uniFeaFactorF;}

std::string event::getLoc(){ return location;}

std::string event::getUF(){ return uniqueFeature;}

static std::ostream &operator<<(std::ostream &out, event &e)
{
    out << "City: " << e.getLoc() << "\nUnique Feature: " << e.getUF();
}

#endif
通过从文件中读取对象正在主函数中生成。我还在下面粘贴文件读取代码:
std::ifstream fin1(eventFileNm);
std::string lineV;
std::vector<string> v;
while (getline(fin1, lineV))
{
    std::istringstream iss(lineV);
    std::string fieldV;
    while (getline(iss, fieldV, ';'))
    {
        v.push_back(fieldV);
    }

    event *b = new event(v[0], v[1], stoi(v[2]), stoi(v[3]));
    eventvec.push_back(b);
}
eventvec vector 被声明为函数中的参数。

最佳答案

问题不在于 .h可能你链接的不对.o或实现此功能的库,或者您没有该功能的实现,或者根本不编译.cpp
安“未定义引用 ” 当我们在程序中引用对象名称(类、函数、变量等)并且链接器尝试在所有链接的对象文件和库中搜索它时找不到它的定义时,就会发生错误。
因此,当链接器找不到链接对象的定义时,它会发出“ undefined reference ”错误。从定义中可以清楚地看出,此错误发生在链接过程的后期阶段。导致“ undefined reference ”错误的原因有多种。
一些可能的原因(更频繁):
#1) 没有为对象 提供定义
这是导致“ undefined reference ”错误的最简单原因。程序员只是忘记定义对象。
#2) 使用对象的错误定义(签名不匹配)
“ undefined reference ”错误的另一个原因是我们指定了错误的定义。我们在程序中使用任何对象,它的定义是不同的。
考虑以下 C++ 程序。这里我们调用了func1()。它的原型(prototype)是int func1()。但其定义与其原型(prototype)不符。如我们所见,函数的定义包含函数的参数。
这样程序在编译时,由于原型(prototype)和函数调用匹配,所以编译成功。但是当链接器试图将函数调用与其定义链接时,它会发现问题并将错误作为“ undefined reference ”发出。
#3) 对象文件未正确链接
此问题还可能导致“ undefined reference ”错误。在这里,我们可能有多个源文件,我们可能会独立编译它们。完成此操作后,对象未正确链接,并导致“ undefined reference ”。
考虑以下两个 C++ 程序。在第一个文件中,我们使用了在第二个文件中定义的“print()”函数。当我们分别编译这些文件时,第一个文件为 print 函数提供了“ undefined reference ”,而第二个文件为 main 函数提供了“ undefined reference ”。
#4) 错误的项目类型
当我们在 Visual Studio 等 C++ IDE 中指定错误的项目类型并尝试做项目不期望的事情时,我们会得到“ undefined reference ”。
#5) 没有图书馆
如果程序员没有正确指定库路径或完全忘记指定它,那么我们会从库中获得程序使用的所有引用的“ undefined reference ”。
#6) 相关文件未编译
程序员必须确保我们事先编译好项目的所有依赖项,以便在编译项目时,编译器找到所有依赖项并编译成功。如果缺少任何依赖项,则编译器会给出“ undefined reference ”。
除了上面讨论的原因之外,“ undefined reference ”错误可能发生在许多其他情况下。但底线是程序员做错了,为了防止这个错误,他们应该被纠正。

关于c++ - 对类构造函数 h 文件的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64478435/

相关文章:

c++ - CUDA 常量内存错误

c++ - 如何在 C++11/98 中使用 <functional> 跳过参数

c++ - 模板函数采用任何仿函数并返回仿函数返回的类型

c++ - CUDA编译器无法编译简单的测试程序

c++ - 如何简化执行模板函数的 switch 语句?

c++ - 返回局部变量的 std::move

c++ - 通过轮换 boost 2 个文件的日志记录

c++ - 我什么时候应该使用 std::async with sync 作为策略?

compiler-errors - Modelica.Fluid.Valves.Valve损坏的模型无法压缩?接收错误 “Function Utilities.regRoot2 not found in scope <X>”

java - 内部类出现奇怪的编译错误