gpt4 book ai didi

c++ - ld : 1 duplicate symbol for architecture x86_64 in Xcode

转载 作者:行者123 更新时间:2023-11-30 03:25:22 25 4
gpt4 key购买 nike

我一直在为这个问题绞尽脑汁。我进行了全面搜索,似乎发现的都是具有相同错误消息的问题,但涉及构建完整的 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

最佳答案

查看错误信息。实际上读它。

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

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

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

关于c++ - ld : 1 duplicate symbol for architecture x86_64 in Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49040047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com