C++没有匹配参数列表的重载函数实例。

huangapple go评论70阅读模式
英文:

C++ no instance of overloaded function matches the argument list

问题

I'm having a problem with the intellisense of visual studio, it marks me that i can't push to a vector of a struct i made to store certain data for a little OpenGL project, i try to pass the data of the new struct i want to add to the vector but it makes an error, but i don't know why, when i create the struct and store in a variable and then push the variable to the vector, it doesn't mark an error, so i don't know what's the problem, when i passing a variable which is an int, it marks me the error, but when i pass a raw number, directly in the constructor of the struct is fine.

struct VertexBufferElement
{
	unsigned int type;
	unsigned int count;
	bool normalize;
};

void funtion(int count)
{
    //the vector of the struct above
	std::vector<VertexBufferElement> elements;

    //This is not allowed
	elements.push_back({GL_FLOAT,count,false });
        
    //This is allowed
    elements.push_back({GL_FLOAT,4,false});
        
    //This is allowed
    struct VertexBufferElement vb = { GL_FLOAT,count,false };
    elements.push_back(vb);
}
英文:

I'm having a problem with the intellisense of visual studio, it marks me that i can't push to a vector of a struct i made to store certain data for a little OpenGL project, i try to pass the data of the new struct i want to add to the vector but it makes an error, but i don't know why, when i create the struct and store in a variable and then push the variable to the vector, it doesn't mark an error, so i don't know whats the problem, when i passing a variable which is an int, it marks me the error, but when i pass a raw number, directly in the constructor of the struct is fine.

struct VertexBufferElement
{
	unsigned int type;
	unsigned int count;
	bool normalize;
};


void funtion(int count)
{
    //the vector of the struct above
	std::vector&lt;VertexBufferElement&gt; elements;

    //This is not allowed
	elements.push_back({GL_FLOAT,count,false });
        
    //This is allowed
    elements.push_back({GL_FLOAT,4,false});
        
    //This is allowed
    struct VertexBufferElement vb = { GL_FLOAT,count,false };
    elements.push_back(vb);
}

答案1

得分: 5

根据您说的:

>// 这是允许的

> struct VertexBufferElement vb = { GL_FLOAT,count,false };

所以您应该能够使用:

elements.emplace_back(GL_FLOAT,count,false);

而不会触发 IntelliSense 错误的重载分辨率问题。

它还会就地构造元素(就像您发布的“允许”示例中一样),所以更整洁 - 我会避免说性能更好,因为差异应该是微不足道的。

英文:

As you said:

>// This is allowed

> struct VertexBufferElement vb = { GL_FLOAT,count,false };

So you should be able to use:

elements.emplace_back(GL_FLOAT,count,false);

without triggering a faulty intellisense error on overload resolution.

It also constructs the element in place (as in the "allowed" example you posted) so it's more tidy - I'll avoid saying better performing here bc the difference should be minuscule

答案2

得分: 2

这是您要求的翻译内容:

因为 count 的类型是 int,所以将其转换为 unsigned int VertexBuffer::count 是一种缩窄转换,在大括号初始化器中是不允许的。

对于常量表达式如 4,是允许的,因为它可以转换为 unsigned int 而不会造成缩窄(它肯定在范围内,即不是负数)。

您需要将参数改为 unsigned int

void function(unsigned int count) { ... }

添加一个强制转换:

void function(int count) {
    elements.push_back({GL_FLOAT, unsigned(count), false });
    struct VertexBufferElement vb = { GL_FLOAT, unsigned(count), false };
}

或者在 C++20/23 模式下使用带括号的初始化器:

void function(int count) {
    elements.push_back(VertexBufferElement(GL_FLOAT, count, false));
    struct VertexBufferElement vb(GL_FLOAT, count, false);
}

(顺便提一下,struct VertexBufferElement vb = { GL_FLOAT,count,false }; 也是“不允许”的,因为存在缩窄,但许多编译器允许它,并仅作为扩展给出警告。)

英文:

It's because count is of type int, so converting it to unsigned int VertexBuffer::count is a narrowing conversion, which isn't allowed in a braced initializer.

It is allowed for constant expressions like 4 because that can be converted to unsigned int without narrowing (it will definitely be in range, i.e. is not negative).

You need to either make the argument unsinged int:

void function(unsigned count) { ... }

Add a cast:

void function(int count) {
    elements.push_back({GL_FLOAT, unsigned(count), false });
    struct VertexBufferElement vb = { GL_FLOAT, unsigned(count), false };
}

Or use a parenthesised initialiser (in C++20/23 mode):

void function(int count) {
    elements.push_back(VertexBufferElement(GL_FLOAT, count, false));
    struct VertexBufferElement vb(GL_FLOAT, count, false);
}

(By the way struct VertexBufferElement vb = { GL_FLOAT,count,false }; is also "not allowed" because of the narrowing, but many compilers do allow it with only a warning as an extension)

huangapple
  • 本文由 发表于 2023年6月26日 16:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定