C++ multiple definition of constructor error

C++ multiple definition of constructor error

Post by WallShadow on Sat Jul 14, 2012 12:03 am
([msg=68036]see C++ multiple definition of constructor error[/msg])

Good evening everyone,

I personally hate asking for debugging help, but as it happens, I'm on my third day in a row of googling solutions with an answer to how to fix this.

I have a C++ error which complains that my fileBufferTransformable::fileBufferTransformable() function has multiple definitions;


This is the code:


fileBuffer/fileBufferTransformable.hpp :
Code: Select all

#ifndef fileBufferTransformable_h
#define fileBufferTransformable_h



class fileBufferTransformable
{

public:

    fileBufferTransformable();

};



fileBufferTransformable::fileBufferTransformable()  // THIS IS THE LINE THE COMPILER IS COMPLAINING ABOUT
{



}



#endif





fileBuffer/fileBufferRef.cpp :
Code: Select all

#ifndef fileBufferRef_h
#define fileBufferRef_h



#include "fileBufferTransformable.hpp"



#endif





main.cpp :
Code: Select all
#include "fileBuffer/fileBufferRef.cpp"


int main()
{

    return 0;

}





Error console:

    obj\Debug\fileBuffer\fileBufferRef.o||In function `fileBufferTransformable':|
    ...\fileBuffer\fileBufferTransformable.hpp|30|multiple definition of `fileBufferTransformable::fileBufferTransformable()'|
    obj\Debug\main.o:...\fileBuffer\fileBufferTransformable.hpp|30|first defined here|
    obj\Debug\fileBuffer\fileBufferRef.o||In function `fileBufferTransformable':|
    ...\fileBuffer\fileBufferTransformable.hpp|30|multiple definition of `fileBufferTransformable::fileBufferTransformable()'|
    obj\Debug\main.o:...\fileBuffer\fileBufferTransformable.hpp|30|first defined here|
    ||=== Build finished: 4 errors, 0 warnings ===|







The goal of this is to have a package of usable files which, just by including fileBuffer/fileBufferRef.cpp, would all be included to make my life easier. I've managed to do this before with a package of classes, but this doesnt seem to work.

The research did get me to understand that somehow, the compiler is compiling the function twice, but I have no idea why. And besides, the file is never included twice, how could that even happen? A partial solution I found was to use inline on the function, which I have tested and found to work, but I'm wondering if there is a better solution. According to people, the inline solution doesn't solve the problem with the compiler reading the function twice, the compiler simply ignores it if it has inline set.

Any plaintext solutions would be greatly appreciated!

-WallShadow <3
User avatar
WallShadow
Contributor
Contributor
 
Posts: 614
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: C++ multiple definition of constructor error

Post by LoGiCaL__ on Sat Jul 14, 2012 1:32 am
([msg=68040]see Re: C++ multiple definition of constructor error[/msg])

Hmmm, try including the header file in main and take out the .cpp and see what that returns.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1063
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


Re: C++ multiple definition of constructor error

Post by WallShadow on Sat Jul 14, 2012 9:18 am
([msg=68050]see Re: C++ multiple definition of constructor error[/msg])

I've tried that aswell; by including the header in main, it still produces the error, but including the header in main AND removing the include header in the Ref.cpp, fixes it. But again, that's not the result I want.
User avatar
WallShadow
Contributor
Contributor
 
Posts: 614
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: C++ multiple definition of constructor error

Post by centip3de on Sat Jul 14, 2012 11:40 am
([msg=68051]see Re: C++ multiple definition of constructor error[/msg])

I have a feeling you're not showing us your entire code... Lame.

Anyway, using g++, I didn't get any error at all. In fact, it ran just as you planned (I think)... So it actually might be a compiler/IDE issue

Here's what I compiled with:

g++ -Wall -c "%f"

And here's what I built with:

g++ -Wall -o "%e" "%f"
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1449
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: C++ multiple definition of constructor error

Post by ghost107 on Sat Jul 14, 2012 12:09 pm
([msg=68052]see Re: C++ multiple definition of constructor error[/msg])

One ptroblem:

Actually this check t is not needed in cpp files(unless you want to make your code cross platform, creating some abstract functions and compiling the source files according to the platform) since in c++ you only include the header files:
Code: Select all
#ifndef fileBufferRef_h
#define fileBufferRef_h
//...

#endif



Another problem:
Is that you define and declare the class in the header files, and only include it in the cpp files. Usually you declare the class in the header files and defne it in the cpp file(only template classes can be declared and defined in the header files).
the class header file
Code: Select all
#ifndef fileBufferTransformable_h
#define fileBufferTransformable_h

class fileBufferTransformable
{

public:

    fileBufferTransformable();

};
#endif



The class source file:
Code: Select all
#include "fileBufferTransformable.hpp"

fileBufferTransformable::fileBufferTransformable()
{

}


main program EntryPoint:
Code: Select all
#include "fileBuffer/fileBufferTransformable.hpp"

int main()
{
    return 0;
}
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: C++ multiple definition of constructor error

Post by WallShadow on Sat Jul 14, 2012 12:14 pm
([msg=68053]see Re: C++ multiple definition of constructor error[/msg])

centip3de wrote:I have a feeling you're not showing us your entire code... Lame.

Anyway, using g++, I didn't get any error at all. In fact, it ran just as you planned (I think)... So it actually might be a compiler/IDE issue

Here's what I compiled with:

g++ -Wall -c "%f"

And here's what I built with:

g++ -Wall -o "%e" "%f"



I purposely commented out and removed any lines that weren't contributing to the problem, I figured it would make your lives easier by focusing the problem. If you want the finished product, just ask, I'm always happy to help.

As for the problem, I'll try and screw around with the project settings a bit to see if its just messed up overall.

-- Sat Jul 14, 2012 12:40 pm --

Ok, I think cent is onto something, seems like the whole thing just hates me completely.

What i did: new project, new files, (almost) same code.

I decided to take ghost's advice and only declare the class in the header, while implementing the functions in an associated cpp file.

code:

fileBuffer\fileBufferTransformable.hpp :

Code: Select all




class fileBufferTransformable
{

public:

    fileBufferTransformable();

    void setFileInputStream(string nFileStream);

    void bufferInputFile();


/*private:

    ofstream xOFileStream;

    ifstream xIFileStream;

    char * aBuffer;

    int mBufferLength;*/

};





fileBuffer\fileBufferTransformable.cpp :

Code: Select all



#include "fileBufferTransformable.hpp"



fileBufferTransformable::fileBufferTransformable() // THIS IS ERROR
{



}








main.cpp

Code: Select all



#include "fileBuffer\fileBufferTransformable.cpp"



int main()
{

    return 0;

}






Build log :
    -------------- Build: Debug in FileBufferTest ---------------

    Compiling: main.cpp
    Compiling: fileBuffer\fileBufferTransformable.cpp
    Linking console executable: bin\Debug\FileBufferTest.exe
    obj\Debug\fileBuffer\fileBufferTransformable.o: In function `fileBufferTransformable':
    D:/CodeBlocks/share/CodeBlocks/MyFiles/Algorithms/FileBufferTest/fileBuffer/fileBufferTransformable.cpp:8: multiple definition of `fileBufferTransformable::fileBufferTransformable()'
    obj\Debug\main.o:D:/CodeBlocks/share/CodeBlocks/MyFiles/Algorithms/FileBufferTest//fileBuffer/fileBufferTransformable.cpp:8: first defined here
    obj\Debug\fileBuffer\fileBufferTransformable.o: In function `fileBufferTransformable':
    D:/CodeBlocks/share/CodeBlocks/MyFiles/Algorithms/FileBufferTest/fileBuffer/fileBufferTransformable.cpp:8: multiple definition of `fileBufferTransformable::fileBufferTransformable()'
    obj\Debug\main.o:D:/CodeBlocks/share/CodeBlocks/MyFiles/Algorithms/FileBufferTest//fileBuffer/fileBufferTransformable.cpp:8: first defined here
    collect2: ld returned 1 exit status
    Process terminated with status 1 (0 minutes, 0 seconds)
    4 errors, 0 warnings



still the same problem, still at the same exact function. I've tried adding the other functions, they give the same exact error.

Tell me truthfully, is it just that Code::Blocks hates me, or just the world in general?
User avatar
WallShadow
Contributor
Contributor
 
Posts: 614
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: C++ multiple definition of constructor error

Post by ghost107 on Sat Jul 14, 2012 1:13 pm
([msg=68054]see Re: C++ multiple definition of constructor error[/msg])

That error is because you try to include the cpp file instead of the header
main.h
Code: Select all
//ERROR is here
#include "fileBuffer\fileBufferTransformable.cpp"

Its better this version:
#include "path\to\fileBufferTransformable.hpp"


In header files are only inline, extern, and function declarations, including a cpp file in the c++ compiler, it will think that the cpp file will be a header file(the code will be the same as declaring and defining all in the header file).

if you want your code old code to work, just declare that constructor inline.
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


Re: C++ multiple definition of constructor error

Post by WallShadow on Sat Jul 14, 2012 2:21 pm
([msg=68056]see Re: C++ multiple definition of constructor error[/msg])

Yup, basicly what ghost said.

I got the hint in the build log where it says
Code: Select all
Compiling: main.cpp
Compiling: fileBuffer\fileBufferTransformable.cpp


thus, it was compiling not one, but 2 copies of fileBufferTransformable.hpp .

I renamed the Ref file as a header and included it as such. Seemingly simple solution for such as pesky and annoying problem >.<




Works perfectly now, thanks for the help everyone!

-WallShadow <3
User avatar
WallShadow
Contributor
Contributor
 
Posts: 614
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests