Chase McCoy
Chase McCoy

Reputation: 1580

Using a C++ class defined in a .h file

I have implemented a class in a .h file. I am trying to import the .h file into my main.cpp and then create and use objects of that class there. However, I am getting this error from the latex version of Xcode:

ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is my code:

class.h:

using namespace std;

class Cents
{
private:
    int m_nCents;

public:
    Cents(int nCents) { m_nCents = nCents; }

    // Add Cents + Cents
    friend Cents operator+(const Cents &c1, const Cents &c2);

    int GetCents() { return m_nCents; }
};

// note: this function is not a member function!
Cents operator+(const Cents &c1, const Cents &c2)
{
    // use the Cents constructor and operator+(int, int)
    return Cents(c1.m_nCents + c2.m_nCents);
}

And here is my main.cpp:

    Cents cCents1(6);
    Cents cCents2(8);
    Cents cCentsSum = cCents1 + cCents2;
    cout << "I have " << cCentsSum .GetCents() << " cents." << endl;

    return 0;

Help?

Edit: here is the entire error message.

duplicate symbol __ZplRK5CentsS1_ in:
    /Users/chasemccoy/Library/Developer/Xcode/DerivedData/Testing_C++-coximuwgddopngcjrkbjugfliqkv/Build/Intermediates/Testing C++.build/Debug/Testing C++.build/Objects-normal/x86_64/operatorOverload.o
    /Users/chasemccoy/Library/Developer/Xcode/DerivedData/Testing_C++-coximuwgddopngcjrkbjugfliqkv/Build/Intermediates/Testing C++.build/Debug/Testing C++.build/Objects-normal/x86_64/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Upvotes: 2

Views: 136

Answers (3)

Maarten Hilferink
Maarten Hilferink

Reputation: 723

Cents operator+(const Cents &c1, const Cents &c2) is defined in multiple code units. In main.cpp and operatorOverload.cpp. Add inline to fix this or move the definition to operatorOverload.cpp, thus:

// note: this function is not a member function!
inline Cents operator+(const Cents &c1, const Cents &c2)
{
    // use the Cents constructor and operator+(int, int)
    return Cents(c1.m_nCents + c2.m_nCents);
}

Upvotes: 1

Tanuki
Tanuki

Reputation: 439

Try to isolate your header using preprocessor directives and never define implementation in the header:

#ifndef CENTS_HPP
#define CENTS_HPP

using namespace std;

class Cents
{
private:
    int m_nCents;

public:
    Cents(int nCents) { m_nCents = nCents; }

    // Add Cents + Cents
    friend Cents operator+(const Cents &c1, const Cents &c2);

    int GetCents() { return m_nCents; }
};

// note: this function is not a member function!
Cents operator+(const Cents &c1, const Cents &c2);

/* Do this in cpp or inline it.

{
    // use the Cents constructor and operator+(int, int)
    return Cents(c1.m_nCents + c2.m_nCents);
} */

#endif  // CENTS_HPP

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311078

It seems that the class definition is used in one more module apart the module with main. In this case operator + will be defined twice. You have to remove the operator definition from the header file and place it in some one module.

Upvotes: 1

Related Questions