如何在单独的头文件中的结构中定义 char* 数组?

发布于 2024-12-18 15:57:41 字数 1000 浏览 2 评论 0原文

我刚刚开始学习结构并将事物分成不同的文件。

目前我有一个像这样的 Main.cpp 文件:

#include <iostream>
    #include "StudentAnswerSheet.hpp"
using std::cout;
using std::endl;

int main(){

    StudentAnswerSheet sheet = {
        "Sally",
        {'a', 'b', 'a', 'd', 'c'}
    };

    cout << sheet.studentName << ":" <<endl;
    for(int i = 0; i <5; i++){
    cout << sheet.studentAnswers[i] << " " ;
    }
    return 0;
}

和一个包含我的 struct StudentAnswerSheet 数据类型的单独头文件:

#include <string>
using std::string;

struct StudentAnswerSheet{
    string studentName;
    char studentAnswers[5];
};

理想情况下我希望能够拥有最多 5 个字符的数组保存学生的答案。我想我可能需要从 char 更改为 char* 以获得一定程度的灵活性,但是当我尝试实现它时,我收到一条错误消息“char [0] 的初始化器太多”,并且不确定如何更改工作表初始化。

如果我切换到 char* 数组,我也不确定跟踪我的数组包含多少元素的最佳方法是什么。如果我用 cin 获取学生的答案,那么我可以跟踪该数字答案最多 5 个,但如果我只是想自己初始化答案,就像我现在进行测试一样,我不确定计算 StudentAnswers 大小的最有效方法是什么,所以对此的任何建议也将非常感激。

感谢您的帮助!

i'm just beignning to learn about structs and seperating things into different files.

At the moment i have a Main.cpp file like so:

#include <iostream>
    #include "StudentAnswerSheet.hpp"
using std::cout;
using std::endl;

int main(){

    StudentAnswerSheet sheet = {
        "Sally",
        {'a', 'b', 'a', 'd', 'c'}
    };

    cout << sheet.studentName << ":" <<endl;
    for(int i = 0; i <5; i++){
    cout << sheet.studentAnswers[i] << " " ;
    }
    return 0;
}

and a separate header file that contains my struct StudentAnswerSheet data type:

#include <string>
using std::string;

struct StudentAnswerSheet{
    string studentName;
    char studentAnswers[5];
};

Ideally i'd like to be able to have an array of UP TO 5 chars to hold student answers. I think i might need to change from char to char* to get a degree of flexibility but when i tried implementing it i got an error message "too many intialisers for char [0]" and wasn't sure how to change the sheet intialization.

I'm also not sure what the best way to go about keeping track of how many elements my array contains if i switch to an array of char*.. if i take in the student answers with cin then i can keep track of the number of answers up to 5 but if i just wanted to initialize the answers myself like i am at the moment for testing im not sure what the most efficent way to calculate the size of studentAnswers would be, so any advice on that would be much appreciate too.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

既然您似乎可以使用 std::string,那么为什么不使用 std::vector 而不是使用 char[5] 或者考虑使用 char* 来提高灵活性?在您的情况下,您可以简单地使用 std::string ,然后将其中的每个字符解释为学生答案

另外,由于 StudentAnswerSheet 不是 POD,这意味着以下内容将给出编译错误,除非您使用 C++11:

//error in C++98, and C++03; ok in C++11
StudentAnswerSheet sheet = {
    "Sally",
    {'a', 'b', 'a', 'd', 'c'}
};

这是我要做的:

struct StudentAnswerSheet
{
    std::string studentName;
    std::string studentAnswers;

    //constructor helps easy-initialization of object!
    StudentAnswerSheet(const std::string & name, const std::string & answers) 
                : studentName(name), studentAnswers(answers) {}
              //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
};            // it is called member-initialization list

然后将其用作:

StudentAnswerSheet sheet("Sally", "abadc");//easy: thanks to the ctor!

std::cout << sheet.studentName << std::endl;
for(size_t i = 0; i < sheet.studentAnswers.size(); ++i)
{
     std::cout << sheet.studentAnswers[i] << " " ;
}

Since it seems you're allowed to use std::string, then why dont you use std::vector<char> instead of using char[5] or thinking of using char* for flexibility? In your case, you can simply use std::string and then interpret the each character in it as student answer.

Also, since StudentAnswerSheet is not POD, which means the following would give compilation error, unless you use C++11:

//error in C++98, and C++03; ok in C++11
StudentAnswerSheet sheet = {
    "Sally",
    {'a', 'b', 'a', 'd', 'c'}
};

Here is what I would do:

struct StudentAnswerSheet
{
    std::string studentName;
    std::string studentAnswers;

    //constructor helps easy-initialization of object!
    StudentAnswerSheet(const std::string & name, const std::string & answers) 
                : studentName(name), studentAnswers(answers) {}
              //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
};            // it is called member-initialization list

then use it as:

StudentAnswerSheet sheet("Sally", "abadc");//easy: thanks to the ctor!

std::cout << sheet.studentName << std::endl;
for(size_t i = 0; i < sheet.studentAnswers.size(); ++i)
{
     std::cout << sheet.studentAnswers[i] << " " ;
}
千笙结 2024-12-25 15:57:41

我认为你不需要切换,使用 char 数组就可以了(或者你可以使用 std::vector)。使用时

char* something[5];

只需初始化指针数组。当你使用时,

char something[5]; 

你会得到指向数组的指针 - “某物

I don't think you need to switch, using array of char is okay(or you can use std::vector). When you use

char* something[5];

you just initialize array of pointers. And when you use

char something[5]; 

you get pointer to array - "something"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文