Dave0504
Dave0504

Reputation: 1107

Multiple definition error in C++, using header files

im new to programming in C++, but have some experience with Java. Ive created a very basic class called Dog.cpp and its header file Dog.hpp. Netbeans will not build the project, giving me an error stating multiple definitions of both the constructor and the getAge function. As far as I am concerned I have declared the constructor and function in the header file, and defined them in the class file. Where have i gone wrong?

Thanks in advance!

Dog.hpp:

#include <iostream>
using namespace std;
class Dog 
{
public:
    Dog(int someAge);           // Constructor
    ~Dog();                     // Destructor
    int getAge() const;         // function prototype
private:
    int itsAge;                 // age variable

};

Dog.cpp:

#include "Dog.hpp"
using namespace std;


Dog::Dog(int anAge) 
{
    cout << "Dog created \n";
}

int Dog::getAge() const
{
    return itsAge;
}

** main.cpp

#include <iostream>
#include "Dog.cpp"

int main() 
{
    Dog aDog(5);
    cout << aDog.getAge();
    return 0;
}

Netbeans output:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/sams_-_hour_10
make[2]: Entering directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/Dog.o.d
g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/Dog.o.d -o build/Debug/GNU-Linux-x86/Dog.o Dog.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/sams_-_hour_10 build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/Dog.o  
build/Debug/GNU-Linux-x86/Dog.o: In function `Dog::Dog(int)':
/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: multiple definition of `Dog::Dog(int)'
build/Debug/GNU-Linux-x86/main.o:/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: first defined here
build/Debug/GNU-Linux-x86/Dog.o: In function `Dog::Dog(int)':
/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: multiple definition of `Dog::Dog(int)'
build/Debug/GNU-Linux-x86/main.o:/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:12: first defined here
build/Debug/GNU-Linux-x86/Dog.o: In function `Dog::getAge() const':
/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:18: multiple definition of `Dog::getAge() const'
build/Debug/GNU-Linux-x86/main.o:/home/david/NetBeansProjects/C++/SAMS - Hour 10/Dog.cpp:18: first defined here
build/Debug/GNU-Linux-x86/main.o: In function `main':
main.cpp:(.text+0x6d): undefined reference to `Dog::~Dog()'
main.cpp:(.text+0x7f): undefined reference to `Dog::~Dog()'
collect2: error: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/sams_-_hour_10] Error 1
make[2]: Leaving directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/david/NetBeansProjects/C++/SAMS - Hour 10'
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 931ms)

Upvotes: 1

Views: 1699

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

It seems that you included the cpp module (Dog.cpp) with the member function definitions in the module with function main. You should include only headers.

Change Main.cpp the following way

#include <iostream>
#include "Dog.hpp"

int main() 
{
    Dog aDog(5);
    cout << aDog.getAge();
    return 0;
}

Take into account that you forgot to set data member itsAge in the construtor. It should look as

Dog::Dog(int anAge) : itsAge( anAge )  
{
    cout << "Dog created \n";
}

And you forgot also to define the destructor.

You could write in the class definition for example the following way

class Dog 
{
public:
    Dog(int someAge);           // Constructor
    ~Dog() = default;           // Destructor
    int getAge() const;         // function prototype
private:
    int itsAge;                 // age variable

};

provided that the compiler supports this specifier.

Upvotes: 5

Dennis
Dennis

Reputation: 231

Try doing either this in your 'Dog.hpp':

#ifndef HEADER_GUARD  // Top of file
#define HEADER_GUARD

/* Code in your class here. */

#endif  // End of file

Or this at top of the file:

#pragma once

The former block of code will tell the preprocessor to only include the code within the guard as long as the header guard definition ("FILENAME_HPP" for instance) is not defined. It immediately gets defined if that conditional passes, so the code should only be loaded once.

The latter block will also work - and possibly be more effective - on most compilers, including g++ and Visual C++. It tells the compiler to process the file only once.

Also, you seem to be missing a definition for your class's destructor.

Another Edit: One more thing, it is very bad programming practice to #include .cpp files. Always #include the header files that are associated with them.

Upvotes: 1

Related Questions