C++ Classes/array of strings question

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
I need to know how to assign a value to a public class member.

Like here is my data structure.

class CFG_Rules {

public:
string LHS;
string RHS[500];
int ruleNumber;
};

I declared this in my main function() {
CFG_Rules myRules[1000];
}

passed it into my void function in main like this.

void printRules(CFG_Rules myRules[], token_type t) {
LHS[l] = current_token;
myRules[l].LHS = LHS[l];
cout <<"LHS "<<LHS[l]<<endl;

nTerms = current_token;
myRules[l].RHS = nTerms;
}

assigning the string value to the array LHS[l] and nTerms works, but it is not working for the data structure.

I have been working on this for hours.

I tried to print a test case and it is empty. how do I assign it a value. i tried strcpy but that gives me an array asking for char arrays. I want to use strings.
 
Ah, what? What's with the text having a 'strike through'?
 
It would help if you actually included your entire void function for us. Like what data type is the LHS array, where is the current_token variable defined, etc.

Have you stepped through your program in debug, yet? It's usually easy to find what you're doing wrong while debugging.
 
You've given us too little information to go on. How are you calling your printRules() function from main()? Where are your type declarations for both arguments and local variables? There is a large assumption that all variable types in printRules are of type string.

As a side note, don't use C library utilities like strcpy on a C++ string, that leads to disaster. If you're intending to use C++ string, then stick with C++ facilities like the copy assignment (=) operator or std::copy. Avoid mix and matching C strings, char arrays, and C++ string when possible.
 
Thanks I figured it out. Why is the bottom part of my Original Post scratched out?
 
Thanks I figured it out. Why is the bottom part of my Original Post scratched out?
Because of the
Code:
[S]
you used in dereferencing your nTerms array. You can avoid that by using code blocks in your post.
 
Back
Top