0

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I'm relatively new to C++ (as you can probably tell by the question) and I've hit a problem. I have two files: Drives.h and Drives.cpp

Drives.h

#pragma once

enum MountMode
{
    User,
    System,
    Both,
    Auto
};

class Drive
{
public:
    Drive(void);
    ~Drive(void);

    BOOL Mount(MountMode mode);
    VOID Unmount(void);
    BOOL IsConnected(void);

    static char* DeviceName;
    static char* DrivePath;
};

class Drives
{
public:
    Drives(void);
    ~Drives(void);
};

and my Drives.cpp:

#include "stdafx.h"
#include "Drives.h"

Drives::Drives(void)
{
    Drive USB0; //Error happening here
}

Drives::~Drives(void)
{
}

The error is saying that the Drives class constructor, destructor and IsConnected() are all unresolved externals. I'm not sure what I'm missing since I set this class up like the one on cplusplus.com

Thanks in advance

Community
  • 1
  • 1
123
  • 5,666
  • 7
  • 25
  • 30

4 Answers4

4

As the error message says, you have not implemented the constructor and destructor of Drive:

Drive::Drive(void) {
    ...
}

Drive::~Drive(void) {
    ...
}

Creating a local variable of class type (as you do in Drive USB0;) will invoke that class' constructor, and the destructor will be invoked at the end of the variable's scope; hence the error.

You should implement the other functions of Drive too - declaring a function in a class declaration is essentially a promise that the function will be implemented somewhere.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
3

Yes, those methods have been declared in the Drive class in your header file, but you haven't actually created a body for these methods.

You must either create a body inline in your header file, create a body in a CPP file, or make sure you are linking with an existing file that defines these methods. Otherwise, the error is right, these methods have not been defined.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
2

An Unresolved External Symbol error usually means you have provided a declaration of a function but not its definition.

In your case, since you declared Drive(void) and ~Drive(void) the compiler removes its defaults and expects your definitions to exist, which they don't, so it throws an error.

As a side note: using void in place of empty parenthesis to mean "This function takes no arguments" is a C-Style definition and should not be used.

Also,do not use #pragma once as a substitute for include guards. It is a Microsoft-Specific construct and is not compatible with other compilers. Use actual include guards instead:

 
#ifndef CLASS_NAME_H
#define CLASS_NAME_H
//CODE HERE
#endif
 
Casey
  • 10,297
  • 11
  • 59
  • 88
1

In the following code you declare two classes(Drive and Drives), but you provide the implementation only for one (Drives)

#pragma once

enum MountMode
{
    User,
    System,
    Both,
    Auto
};

class Drive
{
public:
    Drive(void);
    ~Drive(void);

    BOOL Mount(MountMode mode);
    VOID Unmount(void);
    BOOL IsConnected(void);

    static char* DeviceName;
    static char* DrivePath;
};

class Drives
{
public:
    Drives(void);
    ~Drives(void);
};

To get rid of the error message, you must include an implementation for Drive's class methods. On way to extend your Drives.cpp so that your code may work looks like this:

#include "stdafx.h"
#include "Drives.h"

//Drive class constructor
Drive::Drive(void)
{
   //Add initialization code here. For example:
   DeviceName = "Name";
   DrivePath = "";
}

//Drive class destructor
Drive::~Drive(void)
{
}

//Also add the implementation for Mount
BOOL Drive::Mount(MountMode mode)
{
  //implementation for Mount. For example:
  return FALSE;
}

//Also add the implementation for Mount
VOID Drive::Unmount()
{
  //implementation for Unmount
}

//Also add the implementation for Mount
BOOL Drive::IsConnected()
{
  //implementation for IsConnected.For example:
  return FALSE;
}


//Drives class constructor
Drives::Drives(void)
{
    Drive USB0; //Error happening here
}

//Drives class destructor
Drives::~Drives(void)
{
}

It is also possible if you copy paste-d the code, that you also have the implementation for the Drive class but you save it in another .cpp file, like Drive.cpp. In that case you should either copy all the implementation methods from the other Drive.cpp file to Drives.cpp. Or you should move the declaration of Drive class from Drives.h to Drive.h. In that case you will have clear separation for classes in different files, which is good, but you will have to include Drive.h in the Drives.h file.

Ioan Paul Pirau
  • 2,733
  • 2
  • 23
  • 26