链接错误,我看不出为什么我会收到它

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

本文关键字:为什么 错误 我看 看不出 链接      更新时间:2023-10-16

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

链接器错误的图片

页眉:

#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 downloadn";           break;
    case E_OUTOFMEMORY:              cout << "Out of memory errorn";           break;
    case INET_E_DOWNLOAD_FAILURE:    cout << "Cannot access server datan";     break;
    default:    cout << "Unknown errorn";    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;
}

这就是我初始化类对象并调用函数的方式:

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

提前感谢!

附言如果你发现我在指针或任何与此相关的问题上做错了什么,请向我指出它们,因为我目前正在大学学习我的课程:电脑游戏开发 CPP,这对我来说真的很新(在去那里之前只用过 java)。再次感谢:)

构建可执行文件时,需要链接包含链接错误中报告的函数的特定库或对象文件。

在这些链接错误之前会发生什么?

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

如果您发现我在指针或任何方面做错了什么,请向我指出

一个建议:无论您使用什么编译器,都将警告级别调到最高,并密切观察它报告的所有内容。有几个问题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 downloadn";
        break;
    case E_OUTOFMEMORY:
        cout << "Out of memory errorn";
        break;
    case INET_E_DOWNLOAD_FAILURE:
        cout << "Cannot access server datan";
        break;
    default:
        cout << "Unknown errorn";
        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

出于某种原因,我只能使用默认构造函数初始化类,而不能使用参数初始化构造函数。我现在正试图找出这个问题,但如果其他人有什么话可以帮助我改进我所写的内容,我们将不胜感激。:)

相关文章: