Jump to content

C++ Can't use string in structs?

Go to solution Solved by Nineshadow,

You don't have a constructor for the struct for it to work like that. Also, don't forget to include std::string.

You can, however, do something like this :

struct quiz_question{string var_type;string input_answer;string real_answerquiz_question(string str1 , string str2, string str3) { var_type = str1, input_answer = str2 , real_answer = str3;}};

Or with initialization lists :

struct quiz_question{string var_type;string input_answer;string real_answer;quiz_question(string str1 , string str2, string str3) : var_type(str1) , input_answer(str2) , real_answer(str3){}};

And use it like this:

quiz_question q("hello", "there" , "world");

Or just set those values :

quiz_question q;/* ... */q.var_type="char";

I decided for practice and studying purposes to create a quiz game that helps me learn how many bytes each data type is (e.g. the answer for char would be 1 byte)

My initial plan was this:

#include <iostream>using namespace std;struct quiz_question {    string var_type;    string input_answer;    string real_answer;};int main() {    quiz_question question_one = {        "char",        "",        "",    };}

The idea was to create a structure that I can use for each quiz question. The input_answer will be filled in by cin later, and real_answer will be determined by the sizeof function. If you guess correctly you get a point, if you guess incorrectly you lose a life. However, I  have this issue:

sMWN5.jpg

 

What did I do wrong?

Also, it's okay to have the struct there right?

Link to comment
https://linustechtips.com/topic/471674-c-cant-use-string-in-structs/
Share on other sites

Link to post
Share on other sites

You don't have a constructor for the struct for it to work like that. Also, don't forget to include std::string.

You can, however, do something like this :

struct quiz_question{string var_type;string input_answer;string real_answerquiz_question(string str1 , string str2, string str3) { var_type = str1, input_answer = str2 , real_answer = str3;}};

Or with initialization lists :

struct quiz_question{string var_type;string input_answer;string real_answer;quiz_question(string str1 , string str2, string str3) : var_type(str1) , input_answer(str2) , real_answer(str3){}};

And use it like this:

quiz_question q("hello", "there" , "world");

Or just set those values :

quiz_question q;/* ... */q.var_type="char";

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

 

You don't have a constructor for the struct for it to work like that. Also, don't forget to include std::string.

You can, however, do something like this :

struct quiz_question{string var_type;string input_answer;string real_answerquiz_question(string str1 , string str2, string str3) { var_type = str1, input_answer = str2 , real_answer = str3;}};

And use it like this:

quiz_question q("hello", "there" , "world");

Or just set those values :

quiz_question q;/* ... */q.var_type="char";

Thanks! I have little idea how constructors and destructors work as it is now. I'll have to research them.

Link to post
Share on other sites

Thanks! I have little idea how constructors and destructors work as it is now. I'll have to research them.

I also added a constructor example with an initialization list.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Since your quiz_question is just a bunch of data you could make it a struct instead of a class in which case the code you used would work correctly.

I messed up, I actually just switched it to class to see if it would magically work somehow and forgot about it, it was struct

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×