实现构造函数时出现LNK2019错误

Error LNK2019 while implementing a constructor

本文关键字:LNK2019 错误 构造函数 实现      更新时间:2023-10-16

我在构造函数的实现上遇到了一些麻烦,我无法找出问题所在。我花了很多时间研究这个问题,如果你能给我一些建议,我会很感激的。

主代码为:

#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    Rocket* rocket;
    rocket=new Rocket();
    //LLA* position=rocket->positionLLA;
    return 0;
}

Rocket.h

#pragma once
#include "LLA.h"
class Rocket {
public: 
    Rocket(); // Default constructor
    LLA* positionLLA;
};

Rocket.cpp

#include "stdafx.h"
#include "Rocket.h"
#include <iostream>
using namespace std;
Rocket::Rocket() // Default constructor
{
     // Initialise the position
     positionLLA=new LLA();
}

错误提示:

error LNK2019: unresolved external symbol "public: __thiscall Rocket::Rocket(void)" (??0Rocket@@QAE@XZ) referenced in function _main.

我知道这个错误与没有声明变量有关,但是我认为我已经声明了所有的类和构造函数。

PS:我使用Visual Studio 2008是为了添加依赖项。

我假设LLA在你的。h文件中是正确定义的

你正在编译Rocket.cpp到Rocket。O和main.cpp成main。然后将两个目标文件链接在一起?

您的错误似乎暗示链接器无法从Rocket获取符号信息。o,通常意味着火箭。0未找到

哦,作为一个细节,因为Rocket使用的是LLA*,你不需要在Rocket.h中包含LLA.h,你可以简单地向前声明

class LLA;

您可能希望将#ifndef, #define#endif添加到您的头文件中。在不同的文件中包含多个相同的内容可能会导致复杂性。