【问题标题】:Classes not linking properly类没有正确链接
【发布时间】:2013-11-06 12:06:01
【问题描述】:

我创建了一个简单的类,其中包含头文件 (.h)、类实现文件 (.cpp) 和主文件 (.cpp)。

在我尝试在 x 代码中链接(构建)出现此错误之前似乎没有错误:

架构 x86_64 的未定义符号:
“bbq::bbq(std::__1::basic_string, std::__1::allocator >, std::__1::basic_string, std::__1::allocator >)”,引用自: _main in main.o
ld:未找到架构 x86_64 的符号
clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)*

这是我的代码:

int main()

{

bbq barbeque ("coleman", "101a");


barbeque.loadCoals(); // print output

头文件:

class bbq
{
private:
string brand, model;



public:
bbq (string brand, string model);
void loadCoals();}

以及函数定义:

void bbq::loadCoals()
{

cout<<"Loading Coleman Grill 101A with coals!";

}

【问题讨论】:

    标签: c++ xcode class hyperlink header


    【解决方案1】:

    你没有写 bbq::bbq(string brand, string model);在 .cpp 文件中实现。

    你必须实现它:

    bbq::bbq(string brand, string model) {
       this->brand = brand;
       this->model = model;
    }
    

    【讨论】:

    • 谢谢,我只是在 .cpp 文件中写入,它可以工作。 bbq::bbq(string brand, string model) { } 但是我会在函数括号内放置什么(如果有的话),因为我的其他函数是单独定义的。 @Pierre-Emmanuel-Lallemant
    • 我已经修改了答案;)
    • 谢谢,但是当我输出(
    • std::cout
    • #include "File.h" #include using namespace std; bbq::bbq(字符串品牌,字符串型号){品牌=品牌;型号=型号; } void bbq ::loadCoals() { cout
    猜你喜欢
    • 2016-12-29
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多