gpt4 book ai didi

C++无法添加到模板内的 vector

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:44:50 30 4
gpt4 key购买 nike

我正在学习模板。

我有:

template<typename SQLObject>
std::vector<SQLObject> executeSelectQueryReturnSQLVector(std::string _recordType,
std::string _sql,
int _rowCount)
{
typename std::vector<SQLObject> v;

if (_recordType == AppConstants::sSQLFieldObject)
{
for(int r=0; r < _rowCount; r++)
{
SQLFieldObject o;

o.putFieldNumber(sqlite3_column_int(statement, 0));
[snip]
v.push_back(o);
}
}

if (_recordType == AppConstants::sSQLNotificationObject)
{
for(int r=0; r < _rowCount; r++)
{
SQLNotificationObject o;

o.putNotificationID(sqlite3_column_int(statement, 0));
[snip]
v.push_back(o);
}
}

return v;
}

我在 v.push_back(o); 上遇到编译器错误,它指出:

没有匹配的成员函数来调用“push_back”

我认为这对我来说很模糊,因为这是在编译时确定的类型名,这是有道理的吗?

这是否意味着我必须实现自己的 push_back() 函数?

Vandevoorde 和 Josuttis @ 2003 的“C++ 模板完整指南”是否仍然适用于 C++11?

更新 1:考虑这个编辑:

template<typename SQLObject>
std::vector<SQLObject> executeSelectQueryReturnSQLVector(std::string _recordType,
std::string _sql,
int _rowCount)
{
//typename std::vector<SQLObject> v;

if (_recordType == AppConstants::sSQLFieldObject)
{
std::vector<SQLFieldObject> v;

for(int r=0; r < _rowCount; r++)
{
SQLFieldObject o;

o.putFieldNumber(sqlite3_column_int(statement, 0));
[snip]
v.push_back(o);
}

return v;
}

if (_recordType == AppConstants::sSQLNotificationObject)
{
std::vector<SQLNotificationObject> v;

for(int r=0; r < _rowCount; r++)
{
SQLNotificationObject o;

o.putNotificationID(sqlite3_column_int(statement, 0));
[snip]
v.push_back(o);
}

return v;
}

//return v;
}

我在 return v; 上都遇到了编译器错误,比如:

no viable conversion from 'vector<class SQLFieldObject>' to 'vector<class SQLNotificationObject>'

no viable conversion from 'vector<class SQLNotificationObject>' to 'vector<class SQLFieldObject>'

我这样调用它:

std::vector<SQLFieldObject> _v = executeSelectQueryReturnSQLVector<SQLFieldObject>    (AppConstants::sSQLFieldObject, getSQLToSelectFields(), rowCount);

std::vector<SQLNotificationObject> _v = executeSelectQueryReturnSQLVector<SQLNotificationObject>(AppConstants::sSQLNotificationObject, getSQLToSelectNotifications(), rowCount);

最佳答案

v 的类型是std::vector<SQLObject> . o 的类型是SQLFieldObject .除非有一种自动方式来转换 SQLFieldObject 类型的对象至 SQLObject ,

v.push_back(o);

这是不允许的操作。

更新

与更新代码相关的错误是:

executeSelectQueryReturnSQLVector 的返回类型是std::vector<SQLObject> . return语句返回 std::vector<SQLFieldObject>std::vector<SQLNotificationObject> .嗯,返回对象的类型与函数签名中的返回类型不匹配。

更优雅地处理模板:

 // A template class that returns an appropriate string based on the
// typename used to instantiate.
template <typename SQLObject> struct RecordTypeChooser;

// Specialization for returning the record type for SQLFieldObjects.
template <> struct RecordTypeChooser<SQLFieldObject>
{
static std::string getRecordType() { return AppConstants::sSQLFieldObject; }
};

// Specialization for returning the record type for SQLNotificationObjects.
template <> struct RecordTypeChooser<SQLNotificationObject>
{
static std::string getRecordType() { return AppConstants::sSQLNotificationObject; }
};

// A template class that constructs an object and returns it.
// The object type is based on the typename used to instantiate.
template <typename SQLObject> struct ObjectCreator;

// Specialization for constructing SQLFieldObjects.
template <> struct ObjectCreator<SQLFieldObject>
{
static SQLFieldObject createObject()
{
SQLFieldObject o;
o.putFieldNumber(sqlite3_column_int(statement, 0));
return o;
}
};

// Specialization for constructing SQLNotificationObjects.
template <> struct ObjectCreator<SQLNotificationObject>
{
static SQLNotificationObject createObject()
{
SQLNotificationObject o;
o.putNotificationID(sqlite3_column_int(statement, 0));
return o;
}
};



template<typename SQLObject>
std::vector<SQLObject> executeSelectQueryReturnSQLVector(std::string _recordType,
std::string _sql,
int _rowCount)
{
typename std::vector<SQLObject> v;

// Not sure whether you need this any more.
if (_recordType == RecordTypeChooser<SQLObject>::getRecordType())
{
for(int r=0; r < _rowCount; r++)
{
v.push_back(ObjectCreator<SQLObject>::createObject());
}
}

return v;
}

更新:完全编译和链接的源代码

 #include <vector>
#include <string>

struct SQLFieldObject {};
struct SQLNotificationObject {};

// A template class that returns an appropriate string based on the
// typename used to instantiate.
template <typename SQLObject> struct RecordTypeChooser;

// Specialization for returning the record type for SQLFieldObjects.
template <> struct RecordTypeChooser<SQLFieldObject>
{
static std::string getRecordType() { return "SQLFieldObject"; }
};

// Specialization for returning the record type for SQLNotificationObjects.
template <> struct RecordTypeChooser<SQLNotificationObject>
{
static std::string getRecordType() { return "SQLNotificationObject"; }
};

// A template class that constructs an object and returns it.
// The object type is based on the typename used to instantiate.
template <typename SQLObject> struct ObjectCreator;

// Specialization for constructing SQLFieldObjects.
template <> struct ObjectCreator<SQLFieldObject>
{
static SQLFieldObject createObject()
{
SQLFieldObject o;
// o.putFieldNumber(sqlite3_column_int(statement, 0));
return o;
}
};

// Specialization for constructing SQLNotificationObjects.
template <> struct ObjectCreator<SQLNotificationObject>
{
static SQLNotificationObject createObject()
{
SQLNotificationObject o;
// o.putNotificationID(sqlite3_column_int(statement, 0));
return o;
}
};



template<typename SQLObject>
std::vector<SQLObject> executeSelectQueryReturnSQLVector(std::string _recordType,
std::string _sql,
int _rowCount)
{
typename std::vector<SQLObject> v;

// Not sure whether you need this any more.
if (_recordType == RecordTypeChooser<SQLObject>::getRecordType())
{
for(int r=0; r < _rowCount; r++)
{
v.push_back(ObjectCreator<SQLObject>::createObject());
}
}

return v;
}


void foo()
{
std::vector<SQLFieldObject> v1 = executeSelectQueryReturnSQLVector<SQLFieldObject>("SQLFieldObject",
"",
10);

std::vector<SQLNotificationObject> v2 = executeSelectQueryReturnSQLVector<SQLNotificationObject>("SQLNotificationObject",
"",
10);
}

int main() {}

关于C++无法添加到模板内的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178734/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com