Issues with vectors and referrences

I have the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Form {
   public:
      Form():
         iSize(0),
         iSelection(0),
         vFormLabels(),
         vFormExamples(),
         vFormVariables() {};
      void AddLine() {
         vFormLabels.resize(++ iSize);
         vFormExamples.resize(iSize);
         vFormVariables.resize(iSize);
      };
      void AddLine(std::string label, std::string example, std::string variable) {
         vFormLabels.push_back(label);
         vFormExamples.push_back(example);
         vFormVariables.push_back(variable);
         iSize ++;
      };

      void Play();
   private:
      unsigned int iSize;
      unsigned int iSelection;
      std::vector<std::string> vFormLabels;
      std::vector<std::string> vFormExamples;
      std::vector<std::string> vFormVariables;
};


My issue has become that I have no idea how to store the variable passed to the class member function into the vector. I read on stackoverflow that it wasn't possible, but something similar must be possible. I want to make it as easy as possible for another coder to use my class, so whatever extra code I need to learn to get this working properly is fine with me. I read about using pointers, but pointers with strings don't work very well with my assignments later on. I am also getting errors that the pointer to the string isn't valid with the passed variable.

I figure this was something simple, but I was wrong. I want the user code to look something like:
1
2
3
4
5
6
7
8
9
   string strName;
   string strPhone;
   string strEmail;

   Form myForm;
   myForm.AddLine("Name", "John Doe", strName);
   myForm.AddLine("Phone", "(123) 456-7890", strPhone);
   myForm.AddLine("Email", "username@domain.com", strEmail);
   myForm.Play();


What needs to be changed?
I can't see anything wrong with your approach.

notes:

Do you need the void argument form of AddLine ? Maybe the other version is enough, just use empty strings to add a blank line (could be default arguments). Also the void argument AddLine has some horrible vector resizing in it.

You know that vector has a method size() giving the size of the vector, so you don't need a separate count?

In the AddLine function you are passing the strings by value, push_back then makes another copy to put in the vector. You can avoid a copy and tighten up the definition by passing the strings by const reference.

What I wrote works, but not the way it's intended. I did know about the vector.size(), but the size is used for the form size, not specifically the vectors. I might change that. My issue is that the third parameter for AddLine, variable, is supposed to hold a variable by reference so that when the value is changed in the form, it will change the local variable that passed to that function, which is a reason as to why I don't want to use constants, atleast not there.

Looking at my example, the first AddLine call passes the label = "Name", the example = "John Doe", and the variable = strName. I want to be able to store a bunch of different variables in the form class vector vFormVariables so that I can change the values after the use enters their information. I can't find away around this.

And the empty call to AddLine is for future implementations of when I want to just add a blank line into the form, just to keep the form looking neat. I might change it to having default parameters, but I wrote this fairly early in the morning just to test it and the wall I ran into with is passing the reference to the variables into a vector.
You can store pointers to the variables in the vector.
std::vector<std::string*> vFormVariables;

Pass variable by reference and use operator& to get a pointer to the variable that you can store in the vector.
1
2
3
4
5
6
void AddLine(std::string label, std::string example, std::string& variable) {
	vFormLabels.push_back(label);
	vFormExamples.push_back(example);
	vFormVariables.push_back(&variable);
	iSize ++;
};
Last edited on
I get this error when I attempt to do it:
Code Blocks Compiler wrote:
C:\Programming\lib\form.h|28|error: no matching function for call to 'std::vector<std::basic_string<char>*>::push_back(const string*)'|


That was one of the things I attempted earlier, and I even made sure three times that it was exactly how you had it.
Last edited on
Have you accidentally made variable a const reference?
Yes I did accidentally. It was something I changed earlier and forgot about, but after the corrections, I run into issues with assigning strings to it. I don't know much about pointers, so I tend to have issues when I have to deal with them. This is where I get an error:
std::getline(std::cin, vFormVariables[iSelection], '\n');
And this is my error:
C:\Programming\lib\form.h|62|error: no matching function for call to 'getline(std::istream&, std::basic_string<char>*&, char)'|
Last edited on
You have to dereference the pointer to get the string that the pointer points to.
std::getline(std::cin, *vFormVariables[iSelection], '\n');
Last edited on
Thank you so much Peter. I don't understand why I have such a hard time with pointers when everyone makes it just look so easy. I understand the concept of them, but when it comes to applying them to usable code, I avoid them at all costs since I can't understand them.

I have made the changes to my class and am going to make it look nicer, but the rest of the code should be simple enough for me.
Topic archived. No new replies allowed.