简体   繁体   English

C ++链接器错误(“链接器命令失败,退出代码为1”)

[英]C++ Linker Error (“Linker command failed with exit code 1”)

I'm working on a project and keep getting a linker error, even though there is essentially no executable code thus far. 我正在一个项目上,并不断出现链接器错误,即使到目前为止基本上没有可执行代码。 The error message is below, and from what I gather, it has to do with the overloaded assignment operator, which I have not defined yet. 错误消息在下面,从我收集的内容来看,它与未定义的重载赋值运算符有关。

I can elaborate further through private messages, or if you would like to see the code. 我可以通过私人消息进一步说明,或者如果您想查看代码。 This is due to my school's academic integrity policy, and I do not wish to be expelled! 这是由于我学校的学术诚信政策,我不希望被开除!

Thanks in advance! 提前致谢!


Invoking: MacOS X C++ Linker 调用:MacOS X C ++链接器

g++ -o "ProjectName" ./Class.o ./ProjectName.o g ++ -o“ ProjectName” ./Class.o ./ProjectName.o
Undefined symbols for architecture x86_64: 架构x86_64的未定义符号:

"Class::operator=(Class const&)", referenced from: 从以下位置引用的“ Class :: operator =(Class const&)”

  Class::Class(Class const&) in Class.o

ld: symbol(s) not found for architecture x86_64 ld:找不到架构x86_64的符号

clang: error: linker command failed with exit code 1 (use -v to see invocation) clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

make: *** [ProjectName] Error 1 make:*** [ProjectName]错误1

** UPDATE ** **更新**

If you see "Class" as a class name anywhere that means I forgot to change it back to the real class name, which is ShirtOrder. 如果您在任何地方看到“ Class”作为类名,那意味着我忘记将其更改回真实的类名,即ShirtOrder。

Here is the code in my main cpp file: 这是我的主要cpp文件中的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include "ShirtOrder.h"

using namespace std;


/*********************************************************************************************************************
 ***************** FUNCTION PROTOTYPES -- FUNCTION PROTOTYPES -- FUNCTION PROTOTYPES -- FUNCTION PROTOTYPES **********
 *********************************************************************************************************************/

bool uploadFile(string fName, unsigned long &count, ShirtOrder* &head);
void clearLL(unsigned long &count, ShirtOrder* &head);
void summaryByMethod(unsigned long count, ShirtOrder* head);
void summaryByRegion(unsigned long count, ShirtOrder* head);

/*********************************************************************************************************************
 ***************** MAIN FUNCTION -- MAIN FUNCTION -- MAIN FUNCTION -- MAIN FUNCTION -- MAIN FUNCTION *****************
 *********************************************************************************************************************/

int main(int argc, char* argv[])
{
    if (argc < 2) // if only command line argument is program name
    {
        cout << "No file name entered." << endl;
    }

    else
    {
        ShirtOrder object;
        cout << "Test run" << endl;
    }
    return 0;
}

/**********************************************************************************************************
 ************ FUNCTION IMPLEMENTATIONS -- FUNCTION IMPLEMENTATIONS -- FUNCTION IMPLEMENTATIONS ************
 **********************************************************************************************************/

bool uploadFile(string fName, unsigned long &count, ShirtOrder* &head)
{
    return true;
}

void clearLL(unsigned long &count, ShirtOrder* &head)
{

}

void summaryByMethod(unsigned long count, ShirtOrder* head)
{

}

void summaryByRegion(unsigned long count, ShirtOrder* head)
{

}

my ShirtOrder.h file: 我的ShirtOrder.h文件:

#ifndef SHIRTORDER_H_
#define SHIRTORDER_H_

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

class ShirtOrder
{
    friend ostream& operator<<(ostream &os, const ShirtOrder &rhsObj);

private:
    // the data members below are required (you may change identifiers)
    // there is no need for additional data members
    int orderYear, orderMonth, orderDay;
    char printMethod; // 's', 'i', or 'e'
    string message;
    int mediums; // number of medium shirts ordered
    int larges; // number of large shirts ordered
    int xls; // number of XL shirts ordered
    string shirtColor;
    string inkColor;
    string orderID;
    string region;
    string nameAndEmail;

    ShirtOrder* next; // new for P5

public:
    ShirtOrder(); // default constructor

    ShirtOrder(int orderYear, int orderMonth, int orderDay,
               char printMethod, string message, int mediums, int larges, int xls,
               string shirtColor, string inkColor, string orderID,
               string region, string nameAndEmail,
               ShirtOrder *soPtr = NULL);

    ShirtOrder(const ShirtOrder &otherObj);

    ~ShirtOrder(); // destructor                      // New for P5 (may not be in-line)

    ShirtOrder* getNext() const {return next;}        // New for P5 (may be in-line)

    void setNext(ShirtOrder* soPtr) {next = soPtr;}   // New for P5 (may be in-line)

    ShirtOrder operator=(const ShirtOrder &rhsObj);   // New for P5 (may not be in-line)

    double getBlankCost() const;
    double getPrintingCost() const;
    double getTotalCost() const;

    int    getLetterCount() const;
    char   getPrintMethod() const {return printMethod;}
    int    getOrderYear() const {return orderYear;}
    int    getOrderMonth() const {return orderMonth;}
    int    getOrderDay() const {return orderDay;}
    int    getMediums() const {return mediums;}
    int    getLarges() const {return larges;}
    int    getXls() const{return xls;}

    string getShirtColor() const {return shirtColor;}
    string getInkColor() const {return inkColor;}
    string getOrderID() const {return orderID;}
    string getRegion() const {return region;}
    string getNameAndEmail() const {return nameAndEmail;}
    string getMessage() const {return message;}

    void  setOrderYear (int orderYear) {this->orderYear = orderYear;}
    void  setOrderMonth (int orderMonth) {this->orderMonth = orderMonth;}
    void  setOrderDay (int orderDay) {this->orderDay = orderDay;}
    void  setPrintMethod (char printMethod) {this->printMethod = printMethod;}
    void  setMessage (string message) {this->message = message; }
    void  setMediums (int mediums) {this->mediums = mediums;}
    void  setLarges (int larges) {this->larges = larges;}
    void  setXls (int xls) {this->xls = xls;}
    void  setShirtColor (string shirtColor) {this->shirtColor = shirtColor;}
    void  setInkColor (string inkColor) {this->inkColor = inkColor;}
    void  setOrderID (string orderID) {this->orderID = orderID;}
    void  setRegion (string region) {this->region = region;}
    void  setNameAndEmail (string nameAndEmail) {this->nameAndEmail = nameAndEmail;}

}; // end declaration of class ShirtOrder

/******************************************************************************************
 *********** MEMBER FUNCTION IMPLEMENTATIONS -- MEMBER FUNCTION IMPLEMENTATIONS ***********
 ******************************************************************************************/

#endif /* SHIRTORDER_H_ */

my ShirtOrder.cpp file: 我的ShirtOrder.cpp文件:

#include "ShirtOrder.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

/******************************************************************************************
 *********** MEMBER FUNCTION IMPLEMENTATIONS -- MEMBER FUNCTION IMPLEMENTATIONS ***********
 ******************************************************************************************/

// Default Constructor
ShirtOrder::ShirtOrder()
{
    orderYear = 0;
    orderMonth = 0;
    orderDay = 0;
    printMethod = 'x'; // 's', 'i', or 'e'
    message = "";
    mediums = 0; // number of medium shirts ordered
    larges = 0; // number of large shirts ordered
    xls = 0; // number of XL shirts ordered
    shirtColor = "";
    inkColor = "";
    orderID = "";
    region = "";
    nameAndEmail = "";

    next = NULL;

    cout << "Default constructor has run" << endl;
}

// Other Constructor
ShirtOrder::ShirtOrder(int orderYear, int orderMonth, int orderDay,
        char printMethod, string message, int mediums, int larges, int xls,
        string shirtColor, string inkColor, string orderID,
        string region, string nameAndEmail, ShirtOrder *soPtr)
{
    this-> orderYear = orderYear;
    this->orderMonth = orderMonth;
    this->orderDay = orderDay;
    this->printMethod = printMethod; // 's', 'i', or 'e'
    this->message = message;
    this->mediums = mediums; // number of medium shirts ordered
    this->larges = larges; // number of large shirts ordered
    this->xls = xls; // number of XL shirts ordered
    this->shirtColor = shirtColor;
    this->inkColor = inkColor;
    this->orderID = orderID;
    this->region = region;
    this->nameAndEmail = nameAndEmail;

    next = NULL;

    cout << "Constructor Numero Dos has run" << endl;
}

// Other, other constructor
ShirtOrder::ShirtOrder(const ShirtOrder &otherObj)
{
    ShirtOrder object;
    object = otherObj;
    object.orderYear = otherObj.orderYear;
    object.orderMonth = otherObj.orderMonth;
    object.orderDay = otherObj.orderDay;
    object.printMethod = otherObj.printMethod;
    object.message = otherObj.message;
    object.mediums = otherObj.mediums;
    object.larges = otherObj.larges;
    object.xls = otherObj.xls;
    object.shirtColor = otherObj.shirtColor;
    object.inkColor = otherObj.inkColor;
    object.orderID = otherObj.orderID;
    object.region = otherObj.region;
    object.nameAndEmail = otherObj.nameAndEmail;
    object.next = otherObj.next;

    cout << "Constructor Numero Tres has run" << endl;
}

// DESTRUCTOR
ShirtOrder::~ShirtOrder()
{
    cout << "Destructor has run" << endl;
}

ShirtOrder operator=(const ShirtOrder &rhsObj)
{

}

// COME BACK TO REMOVE WHITESPACE
int ShirtOrder::getLetterCount() const
{
    string tempMessage = getMessage();

    int pos1 = tempMessage.find("\"") + 1; // find first occurrence of a double quotation mark and assign position +1 value to pos1
    int pos2 = tempMessage.rfind("\"") - 1; // find last occurrence of a double quotation mark and assign position -1 value to pos2

    tempMessage = tempMessage.substr(pos1, (pos2 - pos1)); // sets variable tempMessage to string between quotation marks

    return tempMessage.length();
}

double ShirtOrder::getBlankCost() const
{
    return 0.0;
}

double ShirtOrder::getPrintingCost() const
{
    return 0.0;
}

double ShirtOrder::getTotalCost() const
{
    return 0.0;
}

Linker is telling you that Class::Class(Class const&) is missing from your Class.cpp 链接器告诉您Class.cpp中缺少Class::Class(Class const&)

You either need to remove its declaration from your Class.h or add implementation in your Class.cpp. 您需要从Class.h中删除其声明,或者在Class.cpp中添加实现。

The error that you originally reported doesn't quit match with the code. 您最初报告的错误不会与该代码匹配。 Anyways, these are the issues that you have: 无论如何,这些是您遇到的问题:

  • Incorrect return of assignment operator: ShirtOrder operator=(const ShirtOrder &rhsObj) (should be ShirtOrder& ) 赋值运算符的错误返回: ShirtOrder operator=(const ShirtOrder &rhsObj) (应为ShirtOrder&
  • Implementation of operator= is empty, you should assign members and then return *this ; 如果operator =为空,则应该分配成员,然后返回*this ;
  • Your operator= is defined as ShirtOrder& ShirtOrder::operator=(const ShirtOrder &rhsObj) , while it has to be this in .cpp file: ShirtOrder& ShirtOrder::operator=(const ShirtOrder &rhsObj) 您的运算符=定义为ShirtOrder& ShirtOrder::operator=(const ShirtOrder &rhsObj) ,而在.cpp文件中必须是以下内容: ShirtOrder& ShirtOrder::operator=(const ShirtOrder &rhsObj)
  • Your copy constructor ShirtOrder::ShirtOrder(const ShirtOrder &otherObj) doesn't have correct implementation. 您的副本构造函数ShirtOrder::ShirtOrder(const ShirtOrder &otherObj)没有正确的实现。 it has to assign all these values to this-> instead of object. 它必须将所有这些值分配给this->而不是object.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM