user442920
user442920

Reputation: 847

Function returns enum defined in another class (fatal link error)

This seems like a problem that is common. I defined an enum in classA and then included classA in classB. Then, in classB I defined a function which returns the enum type defined in classA...see below. I get the following error:

aFirst.obj : error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)" (?returnsThings@usesTheEnum@@QAE?AW4things_t@justEnum@@XZ) referenced in function _wmain 1>C:\Documents and Settings\Ben\My Documents\Visual Studio 2010\Projects\aFirst\Debug\aFirst.exe : fatal error LNK1120: 1 unresolved externals

#pragma once
class justEnum
{

public:
    justEnum(void);
    ~justEnum(void);
    enum things_t{ONE, TWO};
};



#pragma once
#include "justEnum.h"

class usesTheEnum
{
public:
    usesTheEnum(void);
    ~usesTheEnum(void);
    justEnum::things_t returnsThings(void);
};



#include "StdAfx.h"
#include "usesTheEnum.h"
#include "justEnum.h"

usesTheEnum::usesTheEnum(void)
{
}


usesTheEnum::~usesTheEnum(void)
{
}


justEnum::things_t returnsThings()
{
    return justEnum::ONE;
}



// tester.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "justEnum.h"
#include "usesTheEnum.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    usesTheEnum aUser;
    justEnum anEnum;
    justEnum::things_t anotherEnum;
    anotherEnum = justEnum::ONE;
    aUser.returnsThings();
    cout << anotherEnum;
    return 0;
}

Upvotes: 0

Views: 1768

Answers (3)

chrisaycock
chrisaycock

Reputation: 37930

You need to specify that your definition of returnsThings() is part of the usesTheEnum class.

justEnum::things_t usesTheEnum::returnsThings()
{
    return justEnum::ONE;
}

Upvotes: 2

soynerdito
soynerdito

Reputation: 511

not shure, but can't you just move the enum out of the class?

Or on the .cpp of the class write something like extern enum classname::things_t; just to have the enum added to the generated lib wich is what will be linked against.

Upvotes: 0

error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)"

The compiler is complaining that usesTheEnum::returnThings() is not defined, and I cannot see a definition in the code you posted. You should provide a definition for the function in one translation unit.

I don't think I can emphasize enough how important it to learn to read error messages. The compiler is doing it's best to tell you what is wrong.

Upvotes: 0

Related Questions