linker command failed with exit code 1

Hi, I'm currently working on this code, but it keeps on giving me this error:"linker command failed with exit code 1" and I can't figure out how to get rid of it?

MyInteger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "MyInteger.hpp"
#include <iostream>

MyInteger::MyInteger(int num)
{
    pInteger = new int;
    num = *pInteger;
}

MyInteger::~MyInteger()
{
    delete pInteger;
}

MyInteger::MyInteger(const MyInteger &copy)
{
    pInteger = new int;
    *pInteger = *copy.pInteger;
}

MyInteger  MyInteger::operator=(const MyInteger &overloadCopy)
{
    pInteger = new int;
    *pInteger = *overloadCopy.pInteger;
    return overloadCopy;
}

int MyInteger::getMyInt()
{
    return *pInteger;
}

void MyInteger::setMyInt(int num)
{
    pInteger = new int;
    *pInteger = num;
}


MyIntegerMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "MyInteger.cpp"
#include "MyInteger.hpp"
#include <iostream>

int main()
{
    MyInteger obj1(17);
    MyInteger obj2 = obj1;
    std::cout << obj1.getMyInt() << std::endl;
    std::cout << obj2.getMyInt() << std::endl;
    
    obj2.setMyInt(9);
    std::cout << obj1.getMyInt() << std::endl;
    std::cout << obj2.getMyInt() << std::endl;
    
    MyInteger obj3(42);
    obj2 = obj3;
    std::cout << obj2.getMyInt() << std::endl;
    std::cout << obj3.getMyInt() << std::endl;
    
    obj3.setMyInt(1);
    std::cout << obj2.getMyInt() << std::endl;
    std::cout << obj3.getMyInt() << std::endl;
    
    return 0;
}


Thank you!
Last edited on
MyInteger obj2 = obj1;
On the left, you're trying to use the non-existent default constructor.


While I'm here, num = *pInteger; on line 7 looks wrong
Hi, thanks for the reply.

Shouldn't MyInteger obj2 = obj1 invoke the copy constructor?

and I'm sure why is num = *pInteger wrong?

Thank you!
Shouldn't MyInteger obj2 = obj1 invoke the copy constructor?

It should call the assignment operator.

Why does this class have an int pointer instead of just an int?


but it keeps on giving me this error:"linker command failed with exit code 1"

Read your compile/linker error messages, they should tell you what is wrong.

What does your include file contain?

By the way you shouldn't be #including your source file. Source files should be added to your project/build line to be compiled not #included.


The class has an int pointer instead of just an int because the teacher wanted us to do it that way.... For some reason.

my include file is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef MyInteger_hpp
#define MyInteger_hpp

class MyInteger
{
private:
    int *pInteger;
public:
    MyInteger();
    MyInteger(int);
    ~MyInteger();
    MyInteger(const MyInteger&);
    MyInteger operator=(const MyInteger &);
    void setMyInt(int);
    int getMyInt();
};

#endif /* MyInteger_hpp */ 


And the error messages is

1
2
ld: 11 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Last edited on
ld: 11 duplicate symbols for architecture x86_64

This is probably being caused by #including the source file.


> Shouldn't MyInteger obj2 = obj1 invoke the copy constructor?
>> It should call the assignment operator.
copy constructor.
http://en.cppreference.com/w/cpp/language/copy_constructor
The copy constructor is called whenever an object is initialized
initialization: T a = b; or T a(b);, where b is of type T;



> ld: 11 duplicate symbols for architecture x86_64
don't cut the message. prior to that it should have told you what symbols were duplicated.
also, when asking about linker errors you should show the build command, because the error may be there.

read
http://www.cplusplus.com/forum/general/140198/
http://www.cplusplus.com/forum/general/113904/
> ld: 11 duplicate symbols for architecture x86_64
don't cut the message. prior to that it should have told you what symbols were duplicated.
also, when asking about linker errors you should show the build command, because the error may be there.

This. Use all information. Provide all information. If you do not know what is relevant, then assume that everything is.


A build has multiple steps.
* Compilation: compiler produces an object file for each translation unit.
Within compilation preprocessor prepares translation unit for the compiler.
* Linking: linker combines object files and libraries into binary.

If one gets a linker error, that means that compilation step has succeeded. The compiler has not encountered objectionable syntax (although code can be full of logical errors).


When you see an error, there can be multiple lines of it. (I've heard of "simple" syntax error generating 1000 lines of output.) Focus on the first, not the last. After the first error the system can already be so "off the track" that the rest of the error messages are nonsense.
closed account (SECMoG1T)
>>why is num = *pInteger wrong?

the function of a constructor is to initialize the value of class data, that doesn't
seem to be the case here with "num"

1
2
3
4
5
MyInteger::MyInteger(int num)
{
    pInteger = new int;
    num = *pInteger; /// should be *pInteger = num
}
Last edited on
Thanks guys for all the help, the reason why I was getting that error message was because I included the wrong file. It works fine now.

Thanks again!
Topic archived. No new replies allowed.