Greetings Friends:

I am stuck with a compilation error that I have not been able to resolve. I was able to duplicate this error with a test program so whatever I am doing (or not doing), it is the same. Please see the code below. The error is actually from the linking step. I am receiving an Unresolved External Symbol error. This seems to be from the constructor but I can’t determine where the error is.

Please look at this.

    #include "testType.h"
    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {

        testType<int> something;
        char end;

        cout << "poof";
        cout << "end?";
        cin >> end;

    }







    template<class elemType>
    class testType
    {
    public:
        testType();
    private:
        int nothing;
    };

Regards,

Danni

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Your constructor for "testType" has no implementation. So the linker is looking for one and doesn't find it and thus throws: "unresolved external reference" (these words mean that your "reference" in the code, which is the call to the constructor, is assumed to be "external", i.e. found in another compiled library that is linked to your program, and the linker can't find it, i.e. "unresolved").

You need something like this:

template<class elemType>
class testType
{
public:
testType() {
  //something here.. or nothing, but you need the { } even if it is empty.
};
private:
int nothing;
};

Thank you very much mike_2000_17 !!

Now I can understand the logical part of the code

Regards,

Danni


Your constructor for "testType" has no implementation. So the linker is looking for one and doesn't find it and thus throws: "unresolved external reference" (these words mean that your "reference" in the code, which is the call to the constructor, is assumed to be "external", i.e. found in another compiled library that is linked to your program, and the linker can't find it, i.e. "unresolved").

You need something like this:

template<class elemType>
class testType
{
public:
testType() {
  //something here.. or nothing, but you need the { } even if it is empty.
};
private:
int nothing;
};

Actually, I thought I had that covered. I have pasted the code below with some notes:

testType.h:

#include<iostream>
using namespace std;

template<class elemType>
class testType
{
public:
    testType();            ß  This is the declaration for testType()
private:
    int nothing;
};

template<class elemType>
testType<elemType>::testType()  ß- this is the definition of testType()
{

    cout << "poof";       ßfor my example, all it does is screen write.

}

testTemplate.cpp:

#include "testType.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{

    testType<int> something; ßthis is where the constructor is called.
    char end;

    cout << "poof";
    cout << "end?";
    cin >> end;

}

I thought at first I had a reference issue like before but, in this case it seems to be different. I am wondering now if the definition for testType() is wrong. It does not matter (as far as the error is concerned) if I have the definition in its own .cpp file.

I also thought it had something to do with file location WRT where the compiler thinks where they should be. The files are in the same folder together. I worked around it by combining the implimentation with the declarations in the same header file (or .cpp file)...

What do you think?

Danni

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I'm not sure about the exact problem you have, but I think that reading this FAQ will shed some light on your issue. (You mind as well read that entire page, not just that one question)

Edited 5 Years Ago by mike_2000_17: n/a

This article has been dead for over six months. Start a new discussion instead.