链接错误,我看不出为什么我会收到它
Link error and I can't see why I'm getting it
我知道很多人已经问过这个问题很多次了,但是,我完全迷失在这里。我知道为什么会发生链接器错误,但似乎看不到这里的问题是什么。任何帮助指出我的错误都会很棒!
链接器错误的图片
页眉:
#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
出于某种原因,我只能使用默认构造函数初始化类,而不能使用参数初始化构造函数。我现在正试图找出这个问题,但如果其他人有什么话可以帮助我改进我所写的内容,我们将不胜感激。:)
相关文章:
- 我的字符计数代码计算错误.为什么
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 为什么加载SDF会导致Mobilizer创建闭环错误
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 为什么我在使用void函数时得到错误代码C2276
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 为什么g++在未执行的代码处标记强制转换错误
- 有人知道为什么在开关中使用stoi函数会返回恒定的错误吗
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 为什么错误"permission denied","id returned 1 exit status"仅在 IM 使用 C++ 中的头文件 fstream 时才出现
- 为什么错误:"locate_zone"不是"std::chrono"的成员
- 为什么错误 C6386 缓冲区溢出与 strsafe.h 字符串 Cch 函数
- 为什么错误1400无效的窗口句柄
- 为什么错误LNK2001:在这种情况下未解析的外部符号
- c++为什么错误:no match for 'operator='
- 为什么错误"invalid conversion from 'char' to 'const char*"?