首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >shared_ptr模板的C++11向量

shared_ptr模板的C++11向量
EN

Stack Overflow用户
提问于 2014-03-11 19:43:26
回答 1查看 510关注 0票数 0

我正在尝试调用以下函数模板:

代码语言:javascript
复制
    template<typename T>
    bool select(const std::string& ddbbName,
                const std::string& sql,
                std::vector<std::shared_ptr<T>>& vResultSet,
                SqlErrorInfo& errorInfo);

它是在名为SQLite3Manager的类中定义的。在下面的代码中,这个"select“方法什么也不做(返回”true“的一部分)。我试图简化问题描述,因为问题似乎与我调用/使用/定义该方法的方式有关。

所以main.cpp代码是:

main.cpp

代码语言:javascript
复制
#include <iostream>
#include "ES.h"
#include "SQLiteMgr.h"

int main(int argc, const char * argv[])
{
  // To get an instance of the singleton
  Cucut::SQLite3Manager& _sqliteMgr = Cucut::SQLite3Manager::getInstance();
  std::string ddbbName("Cucut.db");
  std::string sql("SELECT * FROM ES");
  std::vector<std::shared_ptr<Cucut::ES>> vspEs;
  Cucut::SqlErrorInfo sqlErrorInfo;
  // Call the template method for <Cucut::ES> using the instance of the singleton
  bool result = _sqliteMgr.select<Cucut::ES>(ddbbName, sql, vspEs, sqlErrorInfo);

  return result;
}

但我在Xcode5中得到了以下链接错误:

代码语言:javascript
复制
Undefined symbols for architecture x86_64:
"bool Cucut::SQLite3Manager::select<Cucut::ES>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::vector<std::__1::shared_ptr<Cucut::ES>, std::__1::allocator<std::__1::shared_ptr<Cucut::ES> > >&, Cucut::SqlErrorInfo&)", referenced from:
  _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

完整的代码是:

ES.h文件:

代码语言:javascript
复制
#ifndef __TestSharedPtr__ES__
#define __TestSharedPtr__ES__

#include <iostream>

namespace Cucut
{
    class ES
    {
    public:
        ES();

        const unsigned int& getId() const;
        void setId(const unsigned int& id);

    private:
        unsigned int _id;
    };
}

#endif /* defined(__TestSharedPtr__ES__) */

ES.cpp文件:

代码语言:javascript
复制
#include "ES.h"

namespace Cucut
{
    ES::ES() :
    _id(0)
    {
    }

    const unsigned int& ES::getId() const
    {
        return _id;
    }

    void ES::setId(const unsigned int& id)
    {
        _id = id;
    }
}

SQLiteMgr.h文件:

代码语言:javascript
复制
#ifndef __TestSharedPtr__SQLiteMgr__
#define __TestSharedPtr__SQLiteMgr__

#include <iostream>
#include <vector>
#include <memory>

namespace Cucut
{
    struct SqlErrorInfo
    {
        int rc;
        std::string description;
    };

    class SQLite3Manager
    {
    private:

        SQLite3Manager();
        SQLite3Manager(const SQLite3Manager& rs);
        SQLite3Manager(SQLite3Manager&& rs);
        SQLite3Manager& operator = (const SQLite3Manager& rs);
        SQLite3Manager& operator = (SQLite3Manager&& rs);

    public:

        static SQLite3Manager& getInstance();
        template<typename T>
        bool select(const std::string& ddbbName,
                    const std::string& sql,
                    std::vector<std::shared_ptr<T>>& vResultSet,
                    SqlErrorInfo& errorInfo);
    };
}

#endif /* defined(__TestSharedPtr__SQLiteMgr__) */

最后是SQLiteMgr.cpp文件:

代码语言:javascript
复制
#include <memory>
#include <vector>
#include "SQLiteMgr.h"

namespace Cucut
{
    SQLite3Manager::SQLite3Manager()
    {
    }

    SQLite3Manager& SQLite3Manager::getInstance()
    {
        static SQLite3Manager instance;

        return instance;
    }

    template<typename T>
    bool SQLite3Manager::select(const std::string& ddbbName, 
                                const std::string& sql,
                                std::vector<std::shared_ptr<T>>& vResultSet,
                                SqlErrorInfo& errorInfo)
    {
        return true;
    }
}

不要因为名称"SqliteMgr“而分心,因为在前面的例子中,为了简化问题,我删除了对sqlite3的任何类型的引用;因此,我似乎没有以正确的方式调用或定义方法”SqliteMgr“,因为我得到了前面提到的链接错误。

任何帮助都将不胜感激。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-03-11 19:49:28

函数模板定义必须始终在头文件中,以便代码可以在实例化时生成(这里在main中)。如果您不这样做,编译器将期望您手动实例化模板,这就是出现链接器错误的原因。将select函数的主体移动到SQLiteMgr.h,它就可以工作了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22324723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档