0

In main:

void HandleAction(const RandomWriter & rw, string choice)
{
   if(choice == "P")
   {
     string fileInput;
     cout << "Change input file: " << endl;
     cin >> fileInput;
     rw.SetFilename(fileInput);
   }
}

In a RandomWriter class:

void RandomWriter::SetFilename(string filename)
{
 string text = GetFullFile(filename);
 if (text != "")
{
  fullText = text;
  this->filename = filename;
}

/

Why am i getting this error when i try to pass fileInput as a parameter to SetFileName?

Thanks in advance guys!

||=== error: passing 'const RandomWriter' as 'this' argument of 'void RandomWriter::SetFilename(std::string)' discards qualifiers [-fpermissive]|
2

2 Answers 2

1

In the HandleAction function you say that the rw is a reference to a constant RandomWriter object. Then you try to call a member function on the rw object that tries to modify the constant object. That is of course not allowed, you can't modify constant objects.

So the simple solution is to remove the const part of the argument specification:

void HandleAction(RandomWriter & rw, string choice) { ... }
//                ^^^^^^^^^^^^^^^^^
//         Note: No longer constant

On a related note, you should probably use references to constant objects for the strings though, no need to copy them all the time.

Sign up to request clarification or add additional context in comments.

Comments

0

Your RandomWriter parameter rw is declared const in your HandleAction() method, and is thus immutable and unable to be changed by your call to SetFilename().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.