首页 > 解决方案 > comma in function arguments causes strange argument split in codeblocks

问题描述

I'm trying to write a function that can take vectors containing int arrays with a length of 3 as an argument. After a bit of reading I was instructed to do this by putting a std::array into a std::vector. However I cannot seem to figure out how to do this.

Here's my code:

#include <vector>
#include <array>

void example(int unrelated_argument1, std::vector<std::array<int, 3> > argument, int unrelated_argument);

void example(int unrelated_argument1, std::vector<std::array<int, 3> > argument, int unrelated_argument)
{
    for(unsigned int i = 0; i < argument.size(); i++)
    {
        printf("Contents of array number %i are %i %i %i\n", i, argument[i][0],argument[i][1], argument[i][2]);
    }
}

int main()
{
    std::vector<std::array <int, 3> >test_vector;

    std::array<int, 3> test_array1 = {1, 2, 3};
    std::array<int, 3> test_array2 = {10, 20, 30};
    std::array<int, 3> test_array3 = {1000, 2000, 3000};

    test_vector.push_back(test_array1);
    test_vector.push_back(test_array2);
    test_vector.push_back(test_array3);

    example(0, test_vector, 0);
}

I expected the function called example to take 3 arguments here, however my IDE(codeblocks) claims that this results in 4 arguments:

Those being int unrelated_argument1, std::vector<std::array<int, 3> > argumentand int unrelated_argument2.

The second and third arguments here are incorrect: I expected the argument to be std::vector<std::array<int, 3> > argument instead.

What am I doing incorrectly here?

Edit: After a whole bit more reading and research, it seems that this is a known bug in codeblocks. https://sourceforge.net/p/codeblocks/tickets/491/ The code here appears to be correct.

标签: c++c++14codeblocks

解决方案


经过更多研究,这似乎确实是代码块中的一个错误,而不是上面的代码。https://sourceforge.net/p/codeblocks/tickets/491/截止到这篇文章,最新的稳定版(17.12)仍然有这个bug。如果您也遇到此错误,您可能想尝试其中一个夜间构建。


推荐阅读