gpt4 book ai didi

c++ - cpp 对 `class::method' 的 undefined reference

转载 作者:行者123 更新时间:2023-11-28 00:54:34 33 4
gpt4 key购买 nike

当我尝试编译时

$ gcc -lcurl 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

该方法出现在标题和类主体中。到底哪里出了问题?

尝试.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 部分(只需包含 header )。另请注意,您不允许在实现中拥有在声明中不可见的私有(private)成员……这很不幸,但语言就是这样定义的。

关于c++ - cpp 对 `class::method' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12229790/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com