没有适当的构造函数可将“block [0]”转换为“block”。

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

no suitable constructor exists to convert from "block [0]" to "block"

问题

我试图返回一个列表并将列表设置为`SCR_StrPack()`,但它给了我两个错误

```cpp
#include <iostream>
#include <windows.h>
using namespace std;
int SCR_X = 40;
int SCR_Y = 110;
struct block {
    char DISPLAY = ' ';
    int TYPE = 0;
    float DESTRUCTION = 1.0f;
};
void SCR_Gen(block blck[]) {
    for(int i=0; i<sizeof(blck); i++) {
        cout << blck[i].DISPLAY;
    }
}
block SCR_StrPack(const char STR[]) {
    block blocks[] = {};
    for(int i=0; i<sizeof(STR); i++) {
        blocks[i].DISPLAY = STR[i];
    }
    return blocks;
}
int main() {
    block tr[20] = {};
    tr = SCR_StrPack("aaa");
    SCR_Gen(tr);
    return 0;
}

我考虑使用for循环,但它是一次性的。我不想每次都重新输入它,只是为了批量修改列表中的内容。

英文:

I'm trying to return a list ant set a list to SCR_StrPack() but it gives me two errors

#include &lt;iostream&gt;
#include &lt;windows.h&gt;
using namespace std;
int SCR_X = 40;
int SCR_Y = 110;
struct block {
    char DISPLAY = &#39; &#39;;
    int TYPE = 0;
    float DESTRUCTION = 1.0f;
};
void SCR_Gen(block blck[]) {
    for(int i=0; i&lt;sizeof(blck); i++) {
        cout &lt;&lt; blck[i].DISPLAY;
    }
}
block SCR_StrPack(const char STR[]) {
    block blocks[] = {};
    for(int i=0; i&lt;sizeof(STR); i++) {
        blocks[i].DISPLAY = STR[i];
    }
    return blocks;
}
int main() {
    block tr[20] = {};
    tr = SCR_StrPack(&quot;\&quot;\&quot;aaa\&quot;\&quot;&quot;);
    SCR_Gen(tr);
    return 0;
}

I'm thinking about using for loop but it's one-time use. I don't want to re-type it every time just to mass-modify something in the list.

答案1

得分: 2

不能返回数组,也不能使用 sizeof(STR) 来获取字符串字面值的长度。当字符串字面值传递给函数时,它会衰减为指向第一个元素的指针,因此 sizeof(STR) 始终是指针的大小。

你可以使用 std::vector<block> 来存储你的 block,同时可以使用 std::string_view 来获取字符串字面值的大小。

示例:

#include <iostream>
#include <string_view>
#include <vector>

struct block {
    char DISPLAY = ' ';
    int TYPE = 0;
    float DESTRUCTION = 1.0f;
};

void SCR_Gen(const std::vector<block>& blck) {
    for (std::size_t i = 0; i < blck.size(); i++) {
        std::cout << blck[i].DISPLAY;
    }
}

std::vector<block> SCR_StrPack(std::string_view STR) {
    std::vector<block> blocks(STR.size());
    for (std::size_t i = 0; i < STR.size(); i++) {
        blocks[i].DISPLAY = STR[i];
    }
    return blocks;
}

int main() {
    // 使用原始字符串字面值比添加很多反斜杠更简单:
    auto tr = SCR_StrPack(R"("aaa")");
    SCR_Gen(tr);
    return 0;
}

你还可以使用基于范围的 for 循环。示例:

void SCR_Gen(const std::vector<block>& blck) {
    for(auto& blk : blck) {
        std::cout << blk.DISPLAY;
    }
}
英文:

You can't return arrays and you can't do sizeof(STR) to get the length of a string literal. When passed to the function, the string literal decays into a pointer to the first element, so sizeof(STR) will always be the size of that pointer.

You could use a std::vector&lt;block&gt; to store your blocks instead and you could also use a std::string_view to get the string literals size.

Example:

#include &lt;iostream&gt;
#include &lt;string_view&gt;
#include &lt;vector&gt;

struct block {
    char DISPLAY = &#39; &#39;;
    int TYPE = 0;
    float DESTRUCTION = 1.0f;
};

void SCR_Gen(const std::vector&lt;block&gt;&amp; blck) {
    for (std::size_t i = 0; i &lt; blck.size(); i++) {
        std::cout &lt;&lt; blck[i].DISPLAY;
    }
}

std::vector&lt;block&gt; SCR_StrPack(std::string_view STR) {
    std::vector&lt;block&gt; blocks(STR.size());
    for (std::size_t i = 0; i &lt; STR.size(); i++) {
        blocks[i].DISPLAY = STR[i];
    }
    return blocks;
}

int main() {
    // using a raw string literal is simpler than adding a lot of backslashes:
    auto tr = SCR_StrPack(R&quot;(&quot;&quot;aaa&quot;&quot;)&quot;);
    SCR_Gen(tr);
    return 0;
}

You could also use range based for-loops. Example:

void SCR_Gen(const std::vector&lt;block&gt;&amp; blck) {
    for(auto&amp; blk : blck) {
        std::cout &lt;&lt; blk.DISPLAY;
    }
}

huangapple
  • 本文由 发表于 2023年4月7日 03:29:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953117.html
匿名

发表评论

匿名网友

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

确定