0

I'm working on a project, and inside of the project I've separated my own classes and functions into a directory called base_lib. When compiling the project, I keep getting an undefined reference error for one of the classes in this library. I have been trying to find the answer on many different forums and ultimately just decided to ask it myself. The code:

stack.h

#ifndef __STACK_H
#define __STACK_H


namespace stacker
{
/*******
My code here
 *******/
}


#endif

stack.cpp

#include "stack.h"
#include <iostream>

using namespace std;

template <typename var>
stacker::Stack<var>::Stack()
{
    head = nullptr;
    tail = nullptr;
}

fix.h

#ifndef __FIX_H
#define __FIX_H

#include <iostream>
#include <string>


namespace fixes
{
/*******
My code here
 *******/
}

#endif

fix.cpp

#include "fix.h"
#include "base_lib/stack.h"
#include <iostream>
#include <string>


using namespace std;
using namespace stacker;

main.cpp

#include <iostream>
#include "fix.h"
#include "base_lib/functions.h"

using namespace fixes;
using namespace mine;
using namespace std;

The compiler error

C:\...:fix.cpp:(.text+0xa2): undefined reference to `stacker::Stack<char>::Stack()'
...

Does anyone have any tips or tricks?

  • Identifiers with a double underscore, identifiers starting with an underscore followed by a capital letter, and identifiers starting with an underscore in the global namespace are reserved. Using them causes undefined behavior, so change the name of your header guard macros. – eesiraed May 14 '20 at 23:26
  • I just added those in the past 10-15 minutes. Unfortunately, it wasn’t the solution to the problem. – Relatively_Kline May 14 '20 at 23:34
  • Just because it's didn't cause this specific problem doesn't mean it's not *a* problem. Your code has undefined behavior because of this, which means the C++ standard doesn't place any restrictions on the behavior of your program. You never know when using a reserved identifiers might cause some weird cryptic errors because it conflicted with identifiers defined by the system. I've seen that happen here before. – eesiraed May 14 '20 at 23:38

1 Answers1

1

You seem to have a template in a C++ file instead of a header file.

This means the template will not compile, thus the error you are getting.

Read here: Why can templates only be implemented in the header file?

Lev M.
  • 6,088
  • 1
  • 10
  • 23