'C++ varadic arguments - function will not take in any additional arguments
I am working with varadic arguments to try to template a class I am using, and I get an error that the function does not take 3 arguments when I try to call the addComponent function in my class. I am very unsure as to why this is, and I haven't found a fix yet.
Class Code:
template <typename T, typename... Args>
class RenderComponentGroup : public RenderGroupBase
{
public:
using RenderGroupBase::RenderGroupBase;
void iterate() { IterateRenderComps<T>(m_Components); };
void addComponent(std::vector<RenderComponent*>* compPointers, Args... arguments)
{
//Check if any dead
for (int i = 0; i < m_Components.size(); i++)
{
if (m_Components[i].isDead())
{
m_Components.emplace(m_Components.begin() + i, arguments...);
m_Components[i].updatePointer();
return;
}
}
//Resize components array
m_Components.emplace_back(arguments...);
m_Components[m_Components.size() - 1].attachToObject(compPointers);
for (int i = 0; i < m_Components.size(); i++)
{
m_Components[i].updatePointer();
}
}
private:
unsigned int m_ID = 0;
std::vector<T> m_Components;
};
Function called:
std::shared_ptr<RenderComponentGroup<SpriteRender>> spriteGroup(new RenderComponentGroup<SpriteRender>());
//Everything after renderComps is the arguments
spriteGroup->addComponent(&sprite->m_RenderComps, &sprite->m_Sprite, &m_SpriteRenderer);
Error:
Error C2660 'RenderComponentGroup<SpriteRender>::addComponent': function does not take 3 arguments SpaceEngine D:\Repositories\Space Engine\SpaceGame\src\game\states\Overworld.cpp 33
Solution 1:[1]
I found the error just after I posted this, and I feel very stupid.
The arguments were declared at the top of the class, which meant that on creation the args resolved to be empty. Because this is technically valid, it didn't throw an error until I tried to add arguments to the function. Moving template<typname... Args> to the top of the function itself means that it resolves to the arguments in the function call.
Fixed:
template <typename T>
class RenderComponentGroup : public RenderGroupBase
{
public:
template<typename... Args>
void addComponent(std::vector<RenderComponent*>* compPointers, Args... arguments)
{
//Check if any dead
for (int i = 0; i < m_Components.size(); i++)
{
if (m_Components[i].isDead())
{
m_Components.emplace(m_Components.begin() + i, arguments...);
m_Components[i].updatePointer();
return;
}
}
//Resize components array
m_Components.emplace_back(arguments...);
m_Components[m_Components.size() - 1].attachToObject(compPointers);
for (int i = 0; i < m_Components.size(); i++)
{
m_Components[i].updatePointer();
}
}
private:
unsigned int m_ID = 0;
std::vector<T> m_Components;
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Guillaume Racicot |
