0

I have the 5 files as from the examples here - Dec.h, Dec.cpp, decInterface.h, decInterface.cpp and temp.c

How to use c++ objects in c? and Developing C wrapper API for Object-Oriented C++ code with the main in a file called temp.c which calls the cpp code implemented in Dec.cpp with help of interface files.

they all reside in one project in vs2008, I have set the compile as option to default instead of compile as c or compile as c++

I get the following link errors

    1>Compiling...
    1>decInterface.cpp
    1>Generating Code...
    1>Compiling...
    1>temp.c
    1>Generating Code...
    1>Compiling manifest to resources...
    1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
    1>Copyright (C) Microsoft Corporation.  All rights reserved.
    1>Linking...

        1>decInterface.obj : error LNK2005: "public: __thiscall Node::Node(class std::vector<float,class std::allocator<float> >)" (??0Node@@QAE@V?$vector@MV?$allocator@M@std@@@std@@@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall Node::Node(float,float)" (??0Node@@QAE@MM@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall Node::Node(class std::vector<float,class std::allocator<float> >,class std::vector<bool,class std::allocator<bool> >)" (??0Node@@QAE@V?$vector@MV?$allocator@M@std@@@std@@V?$vector@_NV?$allocator@_N@std@@@2@@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall reprVectorsTree::reprVectorsTree(class std::vector<class Node *,class std::allocator<class Node *> >,int)" (??0reprVectorsTree@@QAE@V?$vector@PAVNode@@V?$allocator@PAVNode@@@std@@@std@@H@Z) already defined in Dec.obj
        1>decInterface.obj : error LNK2005: "public: __thiscall reprVectorsT

ree::reprVectorsTree(float * *,int,int)" (??0reprVectorsTree@@QAE@PAPAMHH@Z) already defined in Dec.obj
    1>decInterface.obj : error LNK2005: "private: class std::vector<bool,class std::allocator<bool> > __thiscall reprVectorsTree::binaryForm(int,int)" (?binaryForm@reprVectorsTree@@AAE?AV?$vector@_NV?$allocator@_N@std@@@std@@HH@Z) already defined in Dec.obj
    1>decInterface.obj : error LNK2005: "public: class std::vector<float,class std::allocator<float> > __thiscall reprVectorsTree::decode(class std::vector<bool,class std::allocator<bool> >)" (?decode@reprVectorsTree@@QAE?AV?$vector@MV?$allocator@M@std@@@std@@V?$vector@_NV?$allocator@_N@std@@@3@@Z) already defined in Dec.obj
    1>decInterface.obj : error LNK2005: "public: float * __thiscall reprVectorsTree::decode(int *,int)" (?decode@reprVe

ctorsTree@@QAEPAMPAHH@Z) already defined in Dec.obj

What kind of visual studio project settings should I use? I feel the problem is here? Which files to link? how to link? or the default project settings should work?

Community
  • 1
  • 1

1 Answers1

1

The error messages are telling you that several functions are defined in both Dec.cpp and decInterface.cpp, or in a header included by both source files. Usually, you are only allowed a single definition of a function in a program; this is (one aspect of) the One Definition Rule.

If the definitions are in both source files, then remove them from one of them.

If they are in a header, then you have a choice. You could move the definitions into one (and only one) source file, leaving just the declarations in the header:

// header
class Node {
public:
    Node(std::vector<float>); // declaration
};

// source
Node::Node(std::vector<float>) {
    // definition
}

Or you could define them inline in the header. This relaxes the rule to allow multiple identical definitions:

// header
class Node {
public:
    Node(std::vector<float>); // declaration
};

inline Node::Node(std::vector<float>) {
    // inline definition
}

Or you could define them inside the class definition; this also makes them inline:

// header
class Node {
public:
    Node(std::vector<float>) {
        // inline definition
    }
};
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644