Andy Ibanez
Andy Ibanez

Reputation: 12254

Why is this giving me "Undefined references to constructors and destructors"?

I have the following pieces of code:

Leomedia.h

#include "MusicMetaDatter.h"

#ifndef LEOMEDIA_H
#define LEOMEDIA_H

namespace Leomedia
{
    typedef enum
    {
        read,
        write
    } FileMode;
}

#endif // LEOMEDIA_H

MusicMetaDatter.h

#ifndef MUSICMETADATTER_H
#define MUSICMETADATTER_H

#include "Leomedia.h"
#include <string>

/**
* METADATTER
* @Version 1.0.0a
* @Author: Sergio Andrés Ibañez (Leonnears)
* @Twitter: Leonnears
* @Package: Leomedia
*/

    namespace Leomedia
    {
        typedef enum
        {
            mp3,
            m4a,
            flac,
            ape,
            wavPack,
            aiff,
            wav,
            ogg,
            tta
        } AudioFileType;

        class MusicMetaDatter
        {
            public:
                MusicMetaDatter(std::string fileName, AudioFileType type, FileMode mode);
                virtual ~MusicMetaDatter();

            private:
                std::string md_fileName;
                AudioFileType md_fileType;
                FileMode md_fileMode;
        };
    }

    #endif // MUSICMETADATTER_H

MusicMetaDatter.cpp

#include "MusicMetaDatter.h"
#include <string>

Leomedia::MusicMetaDatter::MusicMetaDatter(std::string fileName, AudioFileType type, FileMode mode)
{
    this -> md_fileName = fileName;
    this -> md_fileType = type;
    this -> md_fileMode = mode;
}

Leomedia::MusicMetaDatter::~MusicMetaDatter()
{
    //dtor
}

MetaDatterTest.cpp

#include <iostream>
#include "Leomedia.h"
#include <string>
using namespace std;

int main()
{
    Leomedia::MusicMetaDatter meta("troll", Leomedia::mp3, Leomedia::read);
    return 0;
}

When I compile MetaDatterTest I get the following errors:

undefined reference to Leomedia::MusicMetaDatter::MusicMetaDatter(std::string, Leomedia::AudioFileType, Leomedia::FileMode)' undefined reference to Leomedia::MusicMetaDatter::~MusicMetaDatter()' undefined reference to Leomedia::MusicMetaDatter::~MusicMetaDatter()'

All the files are in the same directory. I'm using mingw 4.4.1

Can someone help me with this? It has dumbfounded me beyond understanding.

Upvotes: 8

Views: 12038

Answers (3)

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361402

Those are not compilation error, they are linkers error. It is probably because you've done this:

g++ MetaDatterTest.cpp

instead of this,

g++ MetaDatterTest.cpp MusicMetaDatter.cpp 

Try this!


Now I will comment on your coding style:

Leomedia::MusicMetaDatter::MusicMetaDatter(std::string fileName, AudioFileType type, FileMode mode)
{
    this -> md_fileName = fileName;
    this -> md_fileType = type;
    this -> md_fileMode = mode;
}

This is bad style. Leomedia is a namespace, so you've written Leomedia::MusicMetaDatter, but you have not written Leomedia::AudioFileType and Leomedia::FileMode in the parameter list.

The better coding-style is this:

namespace Leomedia
{
   MusicMetaDatter::MusicMetaDatter(std::string fileName, AudioFileType type, FileMode mode)
   {
      this -> md_fileName = fileName;
      this -> md_fileType = type;
      this -> md_fileMode = mode;
   }
}

Upvotes: 2

David Schwartz
David Schwartz

Reputation: 182763

Those are linker errors. It compiled just fine. When you link it, you'll need to link to the code that contains the destructor for MusicMetaDatter or you'll get that error.

Upvotes: 2

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143081

You haven't given your compile command, but I suspect it lacks MusicMetaDatter.cpp in it.

Should be something like

g++ -o MetaDatterTest MetaDatterTest.cpp MusicMetaDatter.cpp 

Upvotes: 13

Related Questions