Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My function isnt taking a string when its suppossed to

So I’m trying to have my Actor function work. However
when I input a string into the parameter I get these errors

Line 100: error: cannot convert ‘Vector<std::__cxx11::basic_string<char> >’ to ‘std::__cxx11::basic_string<char>’

Line 100: Actors.push_back((split(actorData, "\t")));
Line 118: error: no match for call to ‘(Vector<std::__cxx11::basic_string<char> >) (std::__cxx11::basic_string<char>&)’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Line 118: Actors = Actor(ActorData[3]);

vector.h library

Vector.h is practically the same as the standard vector class

txtfile

#include <iostream>
#include <string>
#include <fstream>
#include "vector.h" //You can also use <vector>

using namespace std;



Vector<string> split (string s, string delimiter)
{
  size_t pos_start = 0, pos_end, delim_len = delimiter.length();
  string token;
  Vector<string> res;

  while ((pos_end = s.find (delimiter, pos_start)) != string::npos)
  {
    token = s.substr (pos_start, pos_end - pos_start);
    pos_start = pos_end + delim_len;
    res.push_back (token);
  }

  res.push_back (s.substr (pos_start));
  return res;
}
/*
  movie Data
    - returns data from the txt file and stores them in a vector
  Returns:
    - a vector with movie names and actors
*/
Vector<string> movieData(string &filename)
{
  fstream myFile;
  Vector<string> data;
  string content; //All the content casts and movie title
  myFile.open(filename);
  if(!myFile.good()) //If the file failed
  {
    cout << "File Not Found" << endl;
    exit(1);
  }
  //---------------------------------------
  while(getline(myFile, content, '\n')) //seperates each movie by the line
  {
    data.push_back(content);
  }
  //---------------------------------------
  myFile.close();
  return data;
}
/*
  movie Title
    - a Vector with Movie Titles
  Returns:
    - a Vector only including the movie titles from each
      distinct line
*/
Vector<string> movieTitle(Vector<string> &data)
{
  Vector<string> Titles;
  for(int i = 0; i<data.size(); i++)
  {
    int pos = data[i].find("\t");
    Titles.push_back(data[i].substr(0, pos));
  }
  return Titles;
}

/*
  actor Data
    - a Vector with the Actors that are unsliced
      ex: Oscar Issac\tEthan Hawke
  Returns:
    - a Vector only including the actors and not the title
      from each distinct line
*/
Vector<string> actorData(Vector<string> &data)
{
  Vector<string> actorData;
  string word;
  for(int i = 0; i<data.size(); i++)
  {
    int pos = data[i].find(')');
    data[i].erase(0, pos+2);
    actorData.push_back(data[i].substr(0, data[i].length()-1));
  }
  return actorData;
}
/*
  Actor
    - a Vector with the Actors that are sliced
  Returns:
    - a Vector only including the actors and not the title
      from each distinct line
*/
Vector<string> Actor(string &actorData)
{
  Vector<string> Actors;
  Actors.push_back((split(actorData, "\t"))); //Trying to remove tab from the
  return Actors;                               //previous vector
}

int main()
{
  Vector<string> data; //contains titles and cast seperated by \t
                      //ex: Moon Knight (2022)\tOscar Issac\tEthan Hawke
  Vector<string> Title; //Contains Just the Title
                        //ex: Moon Knight (2022)
  Vector<string> ActorData; //Contais the cast that are unscliced
                            //ex: Oscar Issac\tEthan Hawke
  Vector<string> Actor; //contains the actors that are sliced
                        //ex: "Oscar Issac", "Ethan Hawke"
  string filename = "movies_mpaa.txt";
  data = movieData(filename);
  Title = movieTitle(data);
  ActorData = actorData(data);
  Actors = Actor(ActorData[3]);
  //-------------------------------//
  cout << "The Cast Of: "<< Title[3] << " is:" << endl;
  cout << "Actor: " << Actors << endl;
  //cout << "//--------------------//" << endl;
  //cout << "Data: " << data[2] << endl;


  return 0;
}

I used the debugger and something is wrong within my Actor function, but how come? Everything should be fine since it logically makes sense.

  • Actor accepts a string
  • ActorData is a vector with strings inside
  • Actor(ActorData[3]) should work
  • The split function is also good because it just splices strings

>Solution :

You do this

Vector<string> Actor(string &actorData)
{
  Vector<string> Actors;
  Actors.push_back((split(actorData, "\t"))); //Trying to remove tab from the
  return Actors;                               //previous vector
}

splittig the code up a bit gives

Vector<string> Actor(string &actorData)
{
  Vector<string> Actors;
  Vector<string> splitStrs = split(actorData, "\t");
  Actors.push_back(splitStrs); //Trying to remove tab from the
  return Actors;                               //previous vector
}

You cannot push a vector of strings into a vector of strings. I suspect you want the first one (your comment says you are trying to remove a tab)

So you need

Actors.push_back(splitStrs[0]);  

or maybe you mean to add them all

Actors.insert(Actors.begin(), splitStrs.begin(), stritStes.end());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading