cpp 未定义对"class::method"的引用

cpp undefined reference to `class::method'

本文关键字:method 引用 class 未定义 cpp      更新时间:2023-10-16

当我试图编译时

$gcc-lcrl try.cpp

/tmp/ccJs0k9m.o: In function `main':
try.cpp:(.text+0x2d): undefined reference to `getURL::fetch(char*, char*)'
collect2: ld returned 1 exit status

该方法将显示在标头和类主体中。到底出了什么问题?

try.cpp

#include <curl/curl.h>
#include <curl/easy.h>
#include "getURL.h"
int main(void) {
getURL my_getURL;
my_getURL.fetch("http://stackoverflow.com/", "file");
}

getURL.h

#ifndef _getURL
#define _getURL
class getURL {
public:
    void fetch(char *url, char *filename);
};
#endif 

getURL.cpp

#include <curl/curl.h>
#include <curl/easy.h>
#include <stdio.h>

class getURL {
private CURL *curl;
public getURL() {
    //code
}
public void fetch(char *url, char *filename) {
    //code
}
private size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    //code
}
public ~getURL() {
    //code
}
} //end class

您没有使用正确的实现语法;是

getURL::getURL() {
    //code
}
void getURL::fetch(char *url, char *filename) {
    //code
}
size_t getURL::write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    //code
}
getURL::~getURL() {
    //code
}

请注意,您不能在实现中重复class部分(只需包含标头)。还要注意,实现中不允许有在声明中不可见的私有成员。。。这很不幸,但这就是语言的定义。

您只编译了一个源文件。

更改:

gcc -lcurl try.cpp

至:

gcc -lcurl try.cpp getURL.cpp