Link 错误,我不明白为什么会收到它

Link error and I can't see why I'm getting it

我知道很多人已经多次问过这个问题,但是,我完全迷失在这里。我知道为什么会出现链接器错误,但似乎看不出问题出在哪里。 任何指出我的错误的帮助都会很棒!

Picture of linker errors

Header:

#ifndef DOWNLOAD_H
#define DOWNLOAD_H
#include <Windows.h>

class Download {
public:

    Download();
    Download(const char *URL, const char *FILE_NAME);
    ~Download();

    LPCTSTR getURL() const;
    LPCTSTR getFileName() const;

    void setUrl(const char &URL) const;
    void setFileName(const char &FILE_NAME) const;

    void downloadFile();

private:
    struct data_T;
};
#endif // DOWNLOAD_H

Cpp 文件:

#include "download.h"

struct Download::data_T {
    const LPCTSTR &URL;
    const LPCTSTR &FILE_NAME;
} *data;

Download::Download(){}

Download::Download(const char *URL, const char *FILE_NAME)
{
    &data->FILE_NAME = &FILE_NAME;
    &data->URL = &URL;
}

void Download::downloadFile()
{
    HRESULT hr = URLDownloadToFile (0, &data.URL, &data.FILE_NAME, 0, 0);
    switch (hr)
    {
    case S_OK:                       cout << "Successful download\n";           break;
    case E_OUTOFMEMORY:              cout << "Out of memory error\n";           break;
    case INET_E_DOWNLOAD_FAILURE:    cout << "Cannot access server data\n";     break;
    default:    cout << "Unknown error\n";    break;
    }
    printf("%x",hr);
}

void Download::setUrl(const LPCTSTR &URL) {  &data.URL = URL; }
void Download::setFileName(const LPCTSTR &FILE_NAME) { &data.FILE_NAME =     FILE_NAME; }

LPCTSTR Download::getUrl() const { return &data.URL; }
LPCTSTR Download::getFileName() const { return &data.FILE_NAME; }

Download::~Download()
{
    delete this->data_T;
    delete &data;
}

这就是我初始化 class object 并调用函数的方式:

 Download dl("https://www.dropbox.com/s/rm9pszogafgm3e2/Cache_ver.txt?dl=0", "CACHE");
 dl.downloadFile();

在此先致谢!

P.s 如果您发现我在指针方面做错了什么或与此相关的任何事情,请向我指出,因为我目前正在大学学习 CPP 我的课程:计算机游戏开发,这对我来说真的很新(去那里之前只用过 java)。 再次感谢:)

构建可执行文件时,您需要link包含link错误中报告的函数的特定库或目标文件。

那些 link 错误之前发生了什么?

这一行 delete this->data_T; 不应该编译,所以我的猜测是之前某处报告了一个编译错误,没有为 download.cpp 生成目标文件,然后 link呃抱怨缺少目标文件并报告未解析的函数。

if you see anything that I'm doing wrong with pointers or anything for that matter, please point them out to me

一个建议:无论你使用什么编译器,将警告级别调到最高并密切关注它报告的所有内容。发布的代码中有几个问题 struct data_T;*data 未初始化,&data->FILE_NAME = &FILE_NAME;delete &data;

我修复了我遇到的链接器错误问题 不太确定问题出在哪里,但无论如何这就是我的代码现在的样子。

#include "download.h"
#include <Windows.h>
#include <iostream>
#include <urlmon.h>
#pragma comment(lib, "urlmon.lib")

using namespace std;

Download::Download(){}

Download::Download(const LPCTSTR URL, const LPCTSTR FILE_NAME)
{
    data_T.FILE_NAME = FILE_NAME;
    data_T.URL = URL;
}

void Download::downloadFile()
{
    HRESULT hr = URLDownloadToFile(0, data_T.URL, data_T.FILE_NAME, 0, 0);
    switch (hr)
    {
    case S_OK:
        cout << "Successful download\n";
        break;
    case E_OUTOFMEMORY:
        cout << "Out of memory error\n";
        break;
    case INET_E_DOWNLOAD_FAILURE:
        cout << "Cannot access server data\n";
        break;
    default:
        cout << "Unknown error\n";
        break;
    }
    printf("%x",hr);
}

void Download::setURL(const LPCTSTR URLt) {  data_T.URL = URLt; }
void Download::setFileName(const LPCTSTR FILE_NAMEt) { data_T.FILE_NAME = FILE_NAMEt; }

LPCTSTR Download::getURL() const { return data_T.URL; }
LPCTSTR Download::getFileName() const { return data_T.FILE_NAME; }

Download::~Download(){  delete &data_T; }

class Download
{
public:
    Download();
    Download(const LPCTSTR URL, const LPCTSTR FILE_NAME);
    ~Download();
    LPCTSTR getURL() const;
    LPCTSTR getFileName() const;

    void setURL(const LPCTSTR URL);
    void setFileName(const LPCTSTR FILE_NAME);

    void downloadFile();

private:
        struct URLData {
            LPCTSTR URL;
            LPCTSTR FILE_NAME;
        } data_T;
};

#endif // DOWNLOAD_H

出于某种原因,尽管我只能使用默认构造函数而不是带参数的构造函数来初始化 class。我现在正试图找出问题所在,但如果其他人有任何意见可以帮助我改进我所写的内容,将不胜感激。 :)