Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Getting a compiler error, can not figure out what is causing it. Error C2664 \'v

ID: 3778104 • Letter: G

Question

Getting a compiler error, can not figure out what is causing it.

Error   C2664   'void ReadSentence(std::vector<char,std::allocator<char>> &)': cannot convert argument 1 from 'std::vector<std::string,std::allocator<_Ty>>' to 'std::vector<char,std::allocator<char>> &'

Code:

#include <iostream>

#include <string>

#include <cstring>

#include <cmath>

#include <vector>

#include <algorithm>

using namespace std;

void ReadVector(vector<double> &inVal)
{
   int size;
   cout << "Enter the number of double elements to be read in: ";
   cin >> size;

   double element;
   cout << "Enter elements in vector ";
   for (int i = 0; i < size; i++)
   {
       cin >> element;
       inVal.push_back(element);
   }
   cout << endl;
}

void PrintVect(const vector<double> &dataVect, int maxLine)
{
   int t = 0;
   for (int i = 0; i < dataVect.size(); i++)
   {
       if (t == maxLine)
       {
           t = 0;
           cout << endl;
       }

       cout << dataVect[i] << " ";
       ++t;

   }

   cout << endl;
}


void ReadSentence(vector<char> & Sentence)
{
   string str;
   cout << "Enter a sentence terminated by a '.' ";
   getline(cin, str);

   for (int i = 0; i < str.length() - 1; i++)
   {
       Sentence.push_back(str[i]);
   }
}

void reverseWords(const vector<string> &wordVect, vector<string> &backwards)
{
   for (int i = 0; i < wordVect.size(); i++)
   {
       backwards[i] = wordVect[i];
   }

   reverse(backwards.begin(), backwards.end());
}


int main()
{
   vector<double> inVal;
   ReadVector(inVal);
   int maxLine = 2;
   PrintVect(inVal, maxLine);

   vector<string> Sentence;
   ReadSentence(Sentence); //Where I'm getting the error at
   vector<string> backwards;
   reverseWords(Sentence, backwards);

   return 0;
}

Explanation / Answer

Hi,

I have fixed the issue and highighted the code changes below. It is error free now.

#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
void ReadVector(vector<double> &inVal)
{
int size;
cout << "Enter the number of double elements to be read in: ";
cin >> size;
double element;
cout << "Enter elements in vector ";
for (int i = 0; i < size; i++)
{
cin >> element;
inVal.push_back(element);
}
cout << endl;
}
void PrintVect(const vector<double> &dataVect, int maxLine)
{
int t = 0;
for (int i = 0; i < dataVect.size(); i++)
{
if (t == maxLine)
{
t = 0;
cout << endl;
}
cout << dataVect[i] << " ";
++t;
}
cout << endl;
}

void ReadSentence(vector<char> & Sentence)
{
string str;
cin.ignore();
cout << "Enter a sentence terminated by a '.': ";
getline(cin, str);
for (int i = 0; i < str.length() ; i++)
{
Sentence.push_back(str[i]);
}
}
void reverseWords(const vector<char> &wordVect, vector<char> &backwards)
{
for (int i = 0; i < wordVect.size(); i++)
{
backwards.push_back(wordVect[i]);
}
reverse(backwards.begin(), backwards.end());
}

int main()
{
vector<double> inVal;
ReadVector(inVal);
int maxLine = 2;
PrintVect(inVal, maxLine);
vector<char> Sentence;
ReadSentence(Sentence);
vector<char> backwards;
reverseWords(Sentence, backwards);
return 0;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                   

sh-4.2$ main                                                                                                                                                                                                                                           

Enter the number of double elements to be read in: 3                                                                                                                                                                                                   

Enter elements in vector                                                                                                                                                                                                                               

1.1 2.2 3.3                                                                                                                                                                                                                                            

                                                                                                                                                                                                                                                       

1.1 2.2                                                                                                                                                                                                                                                

3.3                                                                                                                                                                                                                                                    

Enter a sentence terminated by a '.': Suresh Murapaka

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote