【问题标题】:Undefined reference error even though the class is linked即使类已链接,未定义的引用错误
【发布时间】:2014-06-26 14:31:42
【问题描述】:

我正在尝试编译一个 c++ 项目,但我得到了一个

undefined reference to `Webdl::GetURLS(std::string const&)'

错误。我在调用类中的代码是这样的

#include <iostream>
#include "Webdl.h"

int main()
{
    using namespace std;
    try
    {
        string hp= Webdl::GetHP("www.google.de","/webhp?output=search&sclient=psy-ab&btnG=");

        vector<string> urls = Webdl::GetURLS( hp );         
    }
    catch (std::exception& e)    
    {
        string str(e.what());
        cout << "Exception: " << str <<endl;
    }
    return 0;
}

头文件类是:

#ifndef WEBDL_H
#define WEBDL_H

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>

class Webdl
{   
    public:
        static std::string GetHP(std::string url, std::string page); 
        static std::vector<std::string> GetURLS(const std::string &html);
};

#endif

对于我使用的编译

g++  -MM ./test.cpp ./Webdl.cpp > Makefile.dep
g++ -g -Wall -O2 -std=c++11 -march=native    -c -o test.o test.cpp
g++ -g -Wall -O2 -std=c++11 -march=native    -c -o Webdl.o Webdl.cpp
g++   test.o Webdl.o  -lboost_system -o test

我也尝试在 Eclipse 中编译它,但结果相同。我希望你能帮助我。

【问题讨论】:

  • 那么Webdl.cpp 是否包含Webdl::GetURLS(std::string const&amp;) 的定义?
  • 是的:std::vector<:string> GetURLS(const std::string &html) { ... }
  • 不,它没有:它定义了GetURLS,而不是Webdl::GetURLS。

标签: c++ linux eclipse linker


【解决方案1】:

根据您的评论,您定义了一个不同的非成员函数

std::vector<std::string> GetURLS(const std::string &html) { ... }

而不是成员函数

std::vector<std::string> Webdl::GetURLS(const std::string &html) { ... }
                         ^^^^^^^

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2023-03-20
    • 2015-01-23
    • 2014-04-21
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多