继承自 sf::Drawable 的类不能与 std::vector 一起使用

问题描述 投票:0回答:1

我使用SFML图形框架制作了一个非常小的2D游戏引擎。最近尝试将引擎从静态库转换为动态库,但是出现了问题。

看起来继承自

sf::Drawable
的类不能在
std::vector
中使用。

这是一个小例子:

Test.h

#pragma once

#include <SFML/Graphics.hpp>
#include <vector>
#include <memory>

class __declspec(dllexport) Entity : sf::Transformable, sf::Drawable
{
public:
    Entity()
    {
    };

    ~Entity()
    {
    };

public:
    std::vector<Entity> children;
};

Test.cpp

#include "Test.h"

Error

错误 C2672:'std::construct_at':找不到匹配的重载函数

Complete building output

1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\xmemory(698,14): error C2672: 'std::construct_at': no matching overloaded function found
1>(compiling source file '/Window.cpp')
1>    C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\xutility(388,16):
1>    could be '_Ty *std::construct_at(_Ty *const ,_Types ...) noexcept(<expr>)'
1>    C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\xmemory(698,14):
1>    the template instantiation context (the oldest one first) is
1>        C:\Users\jehud\source\repos\Harmony\Harmony\Test.h(19,22):
1>        see reference to class template instantiation 'std::vector<Entity,std::allocator<Entity>>' being compiled
1>        C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\vector(1476,26):
1>        while compiling class template member function 'std::vector<Entity,std::allocator<Entity>> &std::vector<Entity,std::allocator<Entity>>::operator =(const std::vector<Entity,std::allocator<Entity>> &)'
1>            C:\Users\jehud\source\repos\Harmony\Harmony\Test.h(20,1):
1>            see the first reference to 'std::vector<Entity,std::allocator<Entity>>::operator =' in 'Entity::operator ='
1>        C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\vector(1492,9):
1>        see reference to function template instantiation 'void std::vector<Entity,std::allocator<Entity>>::_Assign_counted_range<Entity*>(_Iter,const unsigned __int64)' being compiled
1>        with
1>        [
1>            _Iter=Entity *
1>        ]
1>        C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\vector(1395,32):
1>        see reference to function template instantiation 'Entity *std::_Uninitialized_copy_n<Entity*,std::allocator<Entity>>(_InIt,size_t,Entity *,_Alloc &)' being compiled
1>        with
1>        [
1>            _InIt=Entity *,
1>            _Alloc=std::allocator<Entity>
1>        ]
1>        C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\xmemory(1865,18):
1>        see reference to function template instantiation 'void std::_Uninitialized_backout_al<std::allocator<Entity>>::_Emplace_back<Entity&>(Entity &)' being compiled
1>        C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\xmemory(1779,35):
1>        see reference to function template instantiation 'void std::_Default_allocator_traits<_Alloc>::construct<_Ty,Entity&>(_Alloc &,_Objty *const ,Entity &)' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator<Entity>,
1>            _Ty=Entity,
1>            _Objty=Entity
1>        ]

在没有

__declspec(dllexport)
的情况下构建为静态库时效果很好,但由于某种未知的原因,它不会与
__declspec(dllexport)
一起使用。

c++ dll polymorphism overloading sfml
1个回答
0
投票

我认为你的问题是 从 DLL 导出包含 `std::` 对象(矢量、地图等)的类

DLL 内部使用的向量可能与外部不同,即使定义看起来相同。
内部也可以使用完全不同的 STL。

当您静态链接时,您可以确保所有内容都使用相同的定义。

STL中的“S”可能并不像你想象的那么标准。

© www.soinside.com 2019 - 2024. All rights reserved.