C++ 如何在编译代码时解决意外的std::分配器?;
我定义了一个类的构造函数,如下所示:C++ 如何在编译代码时解决意外的std::分配器?;,c++,googletest,allocator,C++,Googletest,Allocator,我定义了一个类的构造函数,如下所示: struct TestClass { TestClass(std::uint8_t, std::vector<Type>) { //... } }; TEST(name1,name2) { //... Type element; std::vector<Type> lst{element}; TestClass instance(0, lst); //.
struct TestClass
{
TestClass(std::uint8_t, std::vector<Type>)
{
//...
}
};
TEST(name1,name2)
{
//...
Type element;
std::vector<Type> lst{element};
TestClass instance(0, lst);
//...
}
以下是编译消息:
/tmp/ccUzsTfz.o:在函数name1\u name2\u Test::TestBody()中:
test.cpp:(.text+0x130):对
Information::Information(无符号字符,std::vectorcollect2:错误:ld返回了1个退出状态 你的签名是:
struct TestClass
{
TestClass(std::uint8_t, std::vector<Type>)
{
//...
}
};
请发布一个。抱歉,由于保密协议,我无法向您显示完整的代码。在我看来,上面的错误信息足以描述我的情况,你应该有能力理解这个问题。(以上代码是我原始代码的抽象模型)@ChenDong不是完整的代码,而是仍然显示相同错误的最小代码。不,这是不够的。首先,它甚至不是完整构建的消息,但我猜它出现在链接阶段。这意味着错误可能在TestClass的定义中,也可能在您使用它的方式中。您确定uint8_t和signed char在您的系统上是相同的类型(而不仅仅是具有相同的表示形式)?@是的,我更新了一个最小的代码。请帮助我解决这个问题,从编译器的消息中,我使用TestClass::TestClass(签名字符,std::vector
struct TestClass
{
TestClass(std::uint8_t, std::vector<Type>)
{
//...
}
};
TestClass::TestClass(signed char, std::vector< Type>, std::allocator< Type>);
TestClass(signed char, std::vector<Type>)
template<
class T,
class Allocator = std::allocator<T>
> class vector;
#include <iostream>
#include <string>
#include <vector>
#include <memory>
struct Type {};
template <typename T>
class MyBatchAllocator {
};
template <typename TAllocator = std::allocator<Type>>
class TestClass {
public:
TestClass(signed char, const std::vector<Type, TAllocator>&) {
// ...
}
};
int main()
{
// main.cpp
Type element;
std::vector<Type, MyBatchAllocator<Type>> lst({ element });
TestClass<MyBatchAllocator<Type>> tc(0, lst);
}
int main() {
Type element;
std::vector<Type> lst2({ element });
TestClass<> tc2(0, lst2);
// Yes, if TestClass is a template with a single
// defaulted template parameter the empty <> are required.
}
using DefaultTestClass = TestClass<>;
DefaultTestClass tc3(0, lst2);