1

I have been cracking my head over this one. I've searched all over and all I seem to find are issues with the same error messages but involving either building complete iphone apps or dealing with header files, all sorts of stuff.

I was just writing a simple C++ program, I wanted all the classes in one file because i'm to lazy. Not sure if that is the issue. This is for a very simple college assignment, but I can't keep working because Xcode gives me this error that has nothing to do with the actual code (based on what I've read). I haven't messed with anything other than the actual .cpp file, I don't even know how I could've messed this up. I've done multiple assignments the same way and have never run into this issue before.

//
//  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;
}

The file below obviously holds all of the classes that will be used in the 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;




}

I didn't use the header file although I think prob should that might be the issue because I don't declare the classes in there. Which I think that you are suppose to do.

here is the error message that I am receiving.

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
devin
  • 368
  • 1
  • 3
  • 19
  • Possible duplicate of [Duplicate symbols for architecture x86\_64 under Xcode](https://stackoverflow.com/questions/24298144/duplicate-symbols-for-architecture-x86-64-under-xcode) – Mohammad Kanan Feb 28 '18 at 23:03
  • 1
    Don't include cpp files. Include the associated header (usually a .h file) and compile and link the cpp file. – user4581301 Feb 28 '18 at 23:04

2 Answers2

2

Look at the error message. Actually read it.

You have a duplicate symbol. Which duplicate symbol is it?

The duplicate symbol is found in two files. Which two files is it found in?

You told the linker to link two source files, which each contain directly or by inclusion the same function Student::addStudent.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

Looks like you're compiling Classes.cpp twice: once when your main includes it, and once independently. If you look at your link error, you can see two object files (Classes.o and main.o).

Stephen Newell
  • 7,330
  • 1
  • 24
  • 28