简体   繁体   中英

How can i define an array of char* in a struct in a separate header file?

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!

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] << " " ;
}

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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM