0

我有来自示例的 5 个文件 - Dec.h、Dec.cpp、decInterface.h、decInterface.cpp 和 temp.c

如何在 c 中使用 c++ 对象?为面向对象的 C++ 代码开发 C 包装 API, 主要在一个名为 temp.c 的文件中,该文件在接口文件的帮助下调用在 Dec.cpp 中实现的 cpp 代码。

它们都驻留在 vs2008 的一个项目中,我已将 compile as 选项设置为默认值,而不是 compile as c 或 compile as c++

我收到以下链接错误

    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

我应该使用什么样的 Visual Studio 项目设置?我感觉问题出在这里?要链接哪些文件?如何链接?或者默认项目设置应该有效?

4

1 回答 1

1

Dec.cpp错误消息告诉您在and中定义了几个函数decInterface.cpp,或者在两个源文件包含的标头中定义了几个函数。通常,您只能在程序中定义一个函数;这是单一定义规则的(一方面)。

如果定义在两个源文件中,则从其中一个文件中删除它们。

如果它们在标题中,那么您可以选择。您可以将定义移动到一个(并且只有一个)源文件中,只在标题中留下声明:

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

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

或者您可以inline在标题中定义它们。这放宽了规则以允许多个相同的定义:

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

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

或者你可以在类定义中定义它们;这也使它们内联:

// header
class Node {
public:
    Node(std::vector<float>) {
        // inline definition
    }
};
于 2012-08-17T07:32:37.827 回答