生成程序时出现此错误:Apple Mach-O Linker (ld) Error Linker Command failed with exit code 1。通常,当我尝试修复此错误时,其原因是具有主要功能的文件为#include-将文件多次删除。但是,我不认为这次是这种情况。 X Code还向我通知了duplicate symbol _passed in: ranker.oolympic.o

//competitor.h

#ifndef __Olympic_Lab__competitor__
#define __Olympic_Lab__competitor__

#include <iostream>
using namespace std;
 class Competitor {
     char* name = nullptr;
     int laneAssignment;
     float time;
 public:
    Competitor(char n[], int lane);
    ~Competitor();
    void setTime(float f);
    char* getName(){    return name;    }
    int getLane(){      return laneAssignment;    }
    float getTime(){    return time;    }
    void print(){   cout << name << endl;    }
 };
#endif

//competitor.cpp

#include "competitor.h"
Competitor::Competitor(char n[], int lane){
    name = n;
    laneAssignment = lane;
}
Competitor::~Competitor(){
    //does nothing for now
}
void Competitor::setTime(float t){
    time = t;
}

//ranker.h

#ifndef __Olym__ranker__
#define __Olym__ranker__
#include <vector>
#include "competitor.h"
using namespace std;
int passed = 0;
class Ranker {
    bool boolean = true;
public:
    vector<Competitor*> rv;
    Ranker(int lanes);
    ~Ranker();
    int addList(Competitor* c);
    Competitor* getLane(int lane);
    Competitor* getFinish(int finish);
    int getFilled();
};
#endif

//ranker.cpp

#include "ranker.h"
Ranker::Ranker(int lan){
    rv.resize(lan - 1);
    for(int i = 0; i <= rv.size(); i++){
        rv[i] = nullptr;
    }
}
Ranker::~Ranker(){
    for(int i = 0; i <= rv.size(); i++){
        delete rv[i];
    }
}
int Ranker::addList(Competitor *c){
    if(c != NULL && passed <= 4){
        rv[passed++] = c;
        return passed - 1;
    }
    return 0;
}
 Competitor* Ranker::getLane(int lane){
    for(int i = 0; i <= rv.size(); i++){
        if(rv[i]->getLane() == lane && rv[i] != NULL){
            return rv[i];
        }
    }
    return rv[0];
}
Competitor* Ranker::getFinish(int finish){
    if(boolean){
        Competitor *temp = nullptr;
        int highestIndex;
        for(int i = rv.size(); i >= 0; i--){
            highestIndex = i;
            for(int j = i; j >= 0; j--){
                if(rv[j] != nullptr && rv[highestIndex] != nullptr){
                    if(rv[j]->getTime() > rv[highestIndex]->getTime())
                        highestIndex = j;
                }
            }
            temp = rv[i];
            rv[i] = rv[highestIndex];
            rv[highestIndex] = temp;
        }
        delete temp;
        temp = *new Competitor*;
        boolean = false;
    }
    return rv[finish - 1];
}
int Ranker::getFilled(){
    int filled = 0;
    for(int i = 0; i <= rv.size(); i++){
        if(rv[i] != NULL){
            filled++;
        }
    }
     return filled;
}

//olympic.h

#ifndef _Olympic_Lab__olympic__
#define _Olympic_Lab__olympic__
#include "ranker.h"
#endif

//olympic.cpp

#include "olympic.h"

 int main(){
    const int lanes = 4;
    Ranker rank(lanes);
    Competitor* starters[4];
    starters[0] = new Competitor("EmmyLou Harris", 1);
    starters[1] = new Competitor("Nanci Griffith", 2);
    starters[2] = new Competitor("Bonnie Raitt", 3);
    starters[3] = new Competitor("Joni Mitchell", 4);
    starters[0]->setTime((float)12.0);
    starters[1]->setTime((float)12.8);
    starters[2]->setTime((float)11.0);
    starters[3]->setTime((float)10.3);
    for(int i = 0; i < lanes; i++){
        rank.addList(starters[i]);
    }
    cout << "Competitors by lane are:" << endl;
    for(int i = 1; i <= lanes; i++)
        rank.getLane(i)->print();
    cout << "Rankings by finish are:" << endl;
    for(int i = 1; i <= lanes; i++)
        rank.getFinish(i)->print();
    for(int i = 0; i < lanes; i++){
        delete starters[i];
    }
}

如果有人可以协助我准确找到导致此错误的信息,我们将不胜感激。谢谢!

最佳答案

如果您在 header 中声明变量,它将在每个包含 header 的文件中重复。

因此,ranker.h中的int passed = 0;将给您带来很多痛苦。 ranker和olympian都分配了不同的passed,链接器现在不知道哪个是真正传递的。

所以你可能想要的是

extern int passed;

在ranker.h中声明已通过将在某个时候存在(如果尚不存在),因此不需要分配。编译器将愉快地进行并允许您使用passed

然后在ranker.cpp中声明
int passed = 0;

分配passed并满足链接器的要求。现在,您只有一个passed,任何包含ranker.h的人都可以查看和使用它。

如果您需要多个passed,则必须做其他事情,以使它们不具有相同的名称和范围,但这似乎与您的目标不符。

离题:抵制将using namespace std;放在头文件中的冲动。这可能会导致将来出现难以调试的问题。更多内容:Why is "using namespace std" considered bad practice?

关于c++ - 为什么我的代码有一个 “Apple Mach-O Linker Error?”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38711521/

10-09 15:19