clang:错误:链接器命令失败,退出代码1(使用-v查看调用)控制台应用程序

时间:2018-02-22 18:13:26

标签: c++ c++11 linker clang

此代码应该可以工作,但我收到链接器错误。我不知道在哪里解决这个问题。我一直在看可可豆荚,我没有可可豆荚。

//main.cpp
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include "ItemToPurchase.cpp"
    #include "ItemToPurchase.hpp"

int main(int argc, const char * argv[]) {
    // insert code here...
    // create objects
    ItemToPurchase item1;
    //ItemToPurchase item2;

    std::cout << "Item1" << std::endl;
    item1.setName();
    //item1.SetPrice();
    //item1.SetQuantity();


    std::cout << "Item2" << std::endl;
    //item2.setName();
    //item2.SetPrice();
    //item2.SetQuantity();

    //test

    std::cout << std::endl << std::endl;
    //item1.getName();
    //item1.GetPrice();
    //item1.GetQuantity();



    return 0;
}

这是ItemToPurchase类cpp文件

//ItemToPurchase
#include "ItemToPurchase.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>



std::string ItemToPurchase::setName(){
    std::cout << "Enter the item name: " << std::endl;
    std::cin >> ItemToPurchase::ItemName;
    return ItemToPurchase::ItemName;

}
/*
std::string ItemToPurchase::getName() {
    return ItemToPurchase::ItemName;
}

int ItemToPurchase::SetPrice(){
    std::cout << "Enter the item price: " << std::endl;
    std::cin >> ItemToPurchase::ItemPrice;
    return ItemToPurchase::ItemPrice;

}

int ItemToPurchase::GetPrice(){
    return ItemToPurchase::ItemPrice;

}


int ItemToPurchase::SetQuantity() {
    std::cout << "Enter the Quantity: " << std::endl;
    std::cin >> ItemToPurchase::ItemQuantity;
    return 0;
}

int ItemToPurchase::GetQuantity() {
    return ItemToPurchase::ItemQuantity;
}
    */

这里是该cpp文件的头文件,它只有类声明。

#ifndef ItemToPurchase_hpp
#define ItemToPurchase_hpp

#include <stdio.h>
#include <stdlib.h>
#include <string>


class ItemToPurchase {
public:

    std::string setName();
    std::string getName();
    int SetPrice();
    //int GetPrice();
    //int GetQuantity();
    //int SetQuantity();
    //ItemToPurchase();

private:
    std::string ItemName = "none";
    int ItemPrice = 0;
    int ItemQuantity = 0;


} items;









#endif /* ItemToPurchase_hpp */

这是完整的错误

duplicate symbol __ZN14ItemToPurchase7setNameEv in:
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/ItemToPurchase.o
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/main.o
duplicate symbol _items in:
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/ItemToPurchase.o
    /Users/devintripp/Library/Developer/Xcode/DerivedData/zybooksLab4-alhksylvtcikaegvkzyxidlzoyib/Build/Intermediates.noindex/zybooksLab4.build/Debug/zybooksLab4.build/Objects-normal/x86_64/main.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:1)

如果将 main.cpp ItemToPurchase.cpp 分开编译,则setName方法的定义会重复,因为第一个定义位于 main中.cpp (行#include "ItemToPurchase.cpp"包含setName定义的源文件内容,第二个定义在ItemToPurchase.cpp。因此,要解决此问题,您应该从#include "ItemToPurchase.cpp"文件中删除main.cpp

第二个问题,你不能在头文件中定义变量,看看ItemToPurchase

的定义
class ItemToPurchase {
 public:
   //...
} items; // you have defined items variables in header

您应该删除items