英文:
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 <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;
}
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<block>
to store your block
s instead and you could also use a std::string_view
to get the string literals size.
Example:
#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() {
// using a raw string literal is simpler than adding a lot of backslashes:
auto tr = SCR_StrPack(R"(""aaa"")");
SCR_Gen(tr);
return 0;
}
You could also use range based for
-loops. Example:
void SCR_Gen(const std::vector<block>& blck) {
for(auto& blk : blck) {
std::cout << blk.DISPLAY;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论