ld:Xcode中架构x86_64的1个重复符号

时间:2018-02-28 22:57:23

标签: c++

我一直在讨论这个问题。我已经搜索了所有我似乎找到的相同错误消息的问题,但涉及建立完整的iPhone应用程序或处理头文件,各种各样的东西。

我只是在编写一个简单的C ++程序,我希望所有类都在一个文件中,因为我很懒。不确定这是不是问题。这是一个非常简单的大学作业,但我不能继续工作,因为Xcode给了我这个与实际代码无关的错误(基于我读过的内容)。除了实际的.cpp文件之外,我没有搞砸任何其他东西,我甚至不知道我怎么会搞砸了。我以同样的方式完成了多项任务,之前从未遇到过这个问题。

//
//  Devin Shawn Tripp
//  Student ID: 11199100
//  CSCE 1040!
//  Hw2
//
//  Created by Devin Tripp on 2/26/18.
//  Copyright © 2018 Devin Tripp. All rights reserved.
//



// 6 classes - main is not a class


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "Classes.cpp"
using namespace std;



int main(int argc, const char * argv[]) {
    /* Declare Variables */
    //Students stud;
    int choice = 0;



    /* Create Menu */
    while (choice != 4) {
        printf("What do you want to do: \n");
        printf("1: Add new Course \n2: Add New Student \n3: Add Enrollment \n4: Quit \n");

        cin >> choice;


        switch (choice) {
            case 1:
                //do something
                // inventory.additem();
                break;
            case 2:
                //do something
                //stud.addStudent();
                break;
            case 3:
                //do something
                // sales.addOrder();
                break;
            case 4:
                //do something
                exit(0);
                break;


            default:
                break;
        }



    }


    return 0;
}

下面的文件显然包含将在main.cpp中使用的所有类。

//
//  Classes.cpp
//  Hw2
//
//  Created by Devin Tripp on 2/28/18.
//  Copyright © 2018 Devin Tripp. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "Classes.hpp"
using namespace std;

#define CHUNKSIZE 2



class Courses {
public:
private:

};

class Student {
public:
    void setId(int id) { stud_id = id;}
    int getId() { return stud_id;}
    void setName(string na) { name = na; }
    string getName() { return name;}
private:
    int stud_id;
    string name;
};


class Students {
public:
    Students() {stud_cnt = 0; stud_cap = CHUNKSIZE; studs = new Student[CHUNKSIZE];}
    ~Students() { delete [] studs;}
    void addStudent();
private:
    int stud_cnt;
    int stud_cap;
    Student *studs;


};

void Students::addStudent() {
    int id;
    string temp;

    if (stud_cnt == stud_cap) {
        Student *temp;
        temp = new Student[stud_cap + CHUNKSIZE];
        for (int i = 0;i < stud_cnt; i++) {
            temp[i] = studs[i];
        }

        delete [] studs;
        stud_cap += CHUNKSIZE;
        studs = temp;

    }

    printf("Enter a new student ID: ");
    cin >> id; cin.ignore();
    printf("Enter Name: ");
    cin >> temp; cin.ignore();
    cout << " Got data " << temp << endl;

    studs[stud_cnt].setId(id); cout << "Set ID" << endl;
    studs[stud_cnt].setName(temp); cout << "Set Name" << endl;
    stud_cnt += 1; cout << "inc stud count" << endl;




}

我没有使用头文件,虽然我认为问题应该是问题,因为我没有在那里声明类。我认为你应该这样做。

这是我收到的错误消息。

Ld /Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Products/Debug/Hw2 normal x86_64
    cd "/Users/devintripp/Desktop/swift projects/Hw2"
    export MACOSX_DEPLOYMENT_TARGET=10.13
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -L/Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Products/Debug -F/Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Products/Debug -filelist /Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Intermediates.noindex/Hw2.build/Debug/Hw2.build/Objects-normal/x86_64/Hw2.LinkFileList -mmacosx-version-min=10.13 -Xlinker -object_path_lto -Xlinker /Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Intermediates.noindex/Hw2.build/Debug/Hw2.build/Objects-normal/x86_64/Hw2_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Intermediates.noindex/Hw2.build/Debug/Hw2.build/Objects-normal/x86_64/Hw2_dependency_info.dat -o /Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Products/Debug/Hw2

duplicate symbol __ZN8Students10addStudentEv in:
    /Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Intermediates.noindex/Hw2.build/Debug/Hw2.build/Objects-normal/x86_64/Classes.o
    /Users/devintripp/Library/Developer/Xcode/DerivedData/Hw2-dpzrwrfmzkaytqajxohtoaipzmkh/Build/Intermediates.noindex/Hw2.build/Debug/Hw2.build/Objects-normal/x86_64/main.o
ld: 1 duplicate symbol for architecture x86_64

2 个答案:

答案 0 :(得分:1)

查看错误消息。实际上读了它。

您有一个重复的符号。它是哪个重复的符号?

在两个文件中找到重复的符号。它找到了哪两个文件?

您告诉链接器链接两个源文件,每个源文件直接包含或包含相同的函数Student :: addStudent。

答案 1 :(得分:0)

看起来你正在编译Classes.cpp两次:一次是你的主include,一次是独立的。如果您查看链接错误,则可以看到两个目标文件(Classes.omain.o)。