0

I have a header file that states the declaration for each function:

#ifndef TYPECONVERTER_H_INCLUDED
#define TYPECONVERTER_H_INCLUDED

#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>

using namespace std;

unsigned int letter2number(char input);
char number2letter(unsigned int input);

string chararr2string(char *input);
string char2string(char input);

char string2char(string input);
char *string2chararr(string input);

string int2string(int input);
int string2int(string input);
wstring string2wstring(const std::string& input);

LPSTR string2lpstr(string input);

vector<int> intarr2intvector(int input[]);

int *vectorint2intarr(vector<int> input);

vector<string> strarr2vectorstr(string *input);
string *vectorstr2strarr(vector<string> input);

vector<char> chararr2vectorchar(char *input);
char *chararr2vector(vector<char> input);

string wchar2string(WCHAR wc[260]);


#endif

And then the .cpp file here with the actual functions:

#include "stdafx.h"

#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>
#include "TypeConverter.h"

using namespace std;

unsigned int letter2number(char input)
{
    if(input >= 'A' && input <= 'Z')
        return input - 64;
    else if(input >= 'a' && input <= 'z')
        return input - 96;
    return 0;
}

char number2letter(unsigned int input)
{
    if(input >= 1 && input <= 26)
        return input + 96;
    return 'a';
}

string chararr2string(char *input)
{
    string ret(input);
    return ret;
}

string char2string(char input)
{
    string ret = "";
    ret += input;
    return ret;
}
char string2char(string input)
{
    return input[0];
}

char *string2chararr(string input)
{
    char *ret=new char[input.size()+1];
    ret[input.size()]=0;

    memcpy(ret,input.c_str(),input.size());

    return ret;
}
string int2string(int input)
{
    return std::to_string(input);
}

int string2int(string input)
{
    try{
        stoi(input);
        return stoi(input);
    }
    catch(std::invalid_argument){
        return 1;
    }

}

wstring string2wstring(const std::string& input)
{
    std::wstring ret = std::wstring(input.begin(), input.end());
    return ret;
}

LPSTR string2lpstr(string input)
{
    return const_cast<char *>(input.c_str());
}

vector<int> intarr2intvector(int input[])
{
    std::vector<int> ret(input, input + sizeof input / sizeof input[0]);
    return ret;
}

int *vectorint2intarr(vector<int> input)
{
    int *ret = &input[0];
    return ret;
}

vector<string> strarr2vectorstr(string *input)
{
    std::vector<string> ret(input, input + sizeof input / sizeof input[0]);
    return ret;
}

string *vectorstr2strarr(vector<string> input)
{
    string *ret = &input[0];
    return ret;
}

vector<char> chararr2vectorchar(char *input)
{
    std::vector<char> ret(input, input + sizeof input / sizeof input[0]);
    return ret;
}

char *chararr2vector(vector<char> input)
{
    char *ret = &input[0];
    return ret;
}

string wchar2string(WCHAR wc[260])
{

    char ch[260];
    char DefChar = ' ';
    WideCharToMultiByte(CP_ACP,0,wc,-1, ch,260,&DefChar, NULL);

    std::string ss(ch);
    return ss;
}

When I use :

#include "stdafx.h"
#include <iostream>
#include <string>
#include "TypeConverter.h"

using namespace std;

int main()
{
    cout << int2string(2) << endl;
}

It works just fine if it's in the project that the two files were created in. However, when I try to do the same thing in a different program by including "TypeConverter.h" and it gives me a LNK2019 "unresolved external symbol" error. What am I doing wrong?

Sam
  • 7,252
  • 16
  • 46
  • 65
  • Duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Apr 12 '14 at 20:39

1 Answers1

1

The header file just declares the functions. You also need to define them. The linker error is telling you that it cannot find a definition for the functions.

Either compile the source file that defines the functions, or supply to the linker an object file or library that defines them.

Which of these options is right for you depends on how you want to structure your programs. However, my guess is that including the source file is the best way to go. This has the obvious benefit that your programs will always be in sync with the source code.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Did I not define them in the .cpp source file? I searched and searched, and that's what they told me to do... – user3385706 Apr 12 '14 at 20:50
  • You did indeed define them in the cpp source file. But the linker is not seeing those definitions. I guess you need to add the cpp file to your other project. Then it will get compiled, and the resulting object passed to the linker. – David Heffernan Apr 12 '14 at 20:58