C++ Classes/array of strings question

JC0133

Senior member
Nov 2, 2010
201
1
76
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.
 

mv2devnull

Golden Member
Apr 13, 2010
1,490
137
106
Code:
struct Foo {
  int bar[3];
}

int main() {
  int array[3];
  array[0] = 42; // assign to dereferenced element of the array;

  int gaz[3][3];
  gaz[1][2] = 42; // assign to dereferenced element of the array;

  Foo foo;
  foo.bar[1] = 42; // assign to dereferenced element of the member array;

  Foo fubar[7];
  fubar[3].bar[2] = 42;
  // fubar[3] is a Foo element within array fubar
  // .bar[2] is an integer element within member array of a Foo
}
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
Code:
string a = "fish"; // a == "fish"
string b = "shrimp"; // b == "shrimp"

b = a; // b == "fish";
a += " and chips"; // a == "fish and chips";

cout << b << endl; // fish

I'm not sure what you are looking for beyond that. C++ will automatically copy strings when you use the ='s operator.
 

JC0133

Senior member
Nov 2, 2010
201
1
76
I apologize. Not looking for anyone to do my homework. If you feel that way please don't answer. I will not take it personally. I just trying to understand why I was not getting the string assignment. Turns out I was doing it correctly but I was looking out of bounds when I did my test check. Thanks your help.