4

I am trying to take some functions that I've made and tested with in a standalone application. Now that I am trying to make a DLL I am getting a few errors stating that the function has been redefined and that the return parameters don't match. This seems to be the only time I am getting these errors, I've tested it by removing this class and it compiles fine as well as the stand-alone app with a main.cpp referencing these directly. Below are the errors and the h and cpp files:

Error 7 error C2371: 'Parser::parse' : redefinition; different basic types c:\users\seb\documents\visual studio 2005\projects\TestDLL\TestDLL\parser.cpp 17

Error 4 error C2526: 'Parser::parse' : C linkage function cannot return C++ class 'std::vector<_Ty>' c:\users\seb\documents\visual studio 2005\projects\TestDLL\TestDLL\parser.h 28

Error 6 error C2556: 'IDVec Parser::parse(const char *)' : overloaded function differs only by return type from 'void Parser::parse(const char *)' c:\users\seb\documents\visual studio 2005\projects\TestDLL\TestDLL\parser.cpp 17

Also find the .h file and the function from the .cpp as well:

Parser.h

#ifndef PARSER_H
#define PARSER_H

#if defined DLL_EXPORT
#define TESTAPI __declspec(dllexport)
#else
#define TESTAPI __declspec(dllimport)
#endif

#include <iostream>
#include <vector>

typedef struct _ListEntry {
    std::string id, path;
} ListEntry;

typedef std::vector<ListEntry> IDVec;

extern "C"
{
    class TESTAPI Parser
    {
    public:
        Parser(void);
        ~Parser(void);
        static IDVec parse(const char* Buffer);
    private:
        static size_t nextLine(std::string& rstrText, size_t pos);
        static std::string nextWord(std::string& rstrText, size_t pos);
        static void fixOSSpecificPath(std::string& rstrPath);
    };
}

#endif

Parser.cpp

IDVec Parser::parse(const char* Buffer) 
{

    std::string s = Buffer;
    IDVec v;

    // Doing stuff here

    return v;
}

Thanks for any advice

Seb
  • 3,414
  • 10
  • 73
  • 106

1 Answers1

3

Remove extern "C" around your class definition.

hamstergene
  • 24,039
  • 5
  • 57
  • 72
  • This did seem to work. I do have a follow-up question to this though. Most of the DLL creation tutorials show classes in an extern "C" block. Is there any use for this at all or is it not necessary. – Seb Aug 31 '11 at 18:36
  • That are some weird tutorials. Blindly wrapping C++ classes with extern "C" makes no sense. There is good explanation what extern "C" does here: http://stackoverflow.com/questions/1041866/extern-c – hamstergene Aug 31 '11 at 18:44