0

My question is: Why does my changepassage (passage,word); function located in main() give me an error for "no matching function"?

my code is supposed to pass in strings titled passage and word from main() to a function void changepassage(string passage,string word). What I'm trying to do with changepassage() is turn the string passage ="..." into string word (I'm still working on removing commas and periods) with each word from the passage assigned it's own element.

//Function prototypes
void changepassage (string,string&);

int main() 
{
  const string passage = "...";

  string word[200];
  
  changepassage(passage,word);
}

This function changepassage() is being labeled as "not matching function" when I try and run the code, but I don't see what the issue is.

The reason I want to make this new string with word is because I want to make another function that will go through the string and tell me how many times each word in an entire passage occurs.

void changepassage (string passage,string word) 
{
  string temp;
  int s = 0;
  
  int plength = passage.length();
  string store[plength];
  

  for (int a = 0; a < plength; a++) 
  {
    if ((isalpha(passage[a]))  || (passage[a] !=' ' && passage[a] !='.' && passage[a] !=',' && passage[a] !=';')) 
    {
      temp += passage[a];
      
    }
    else if (ispunct(passage[a]) || isspace(passage[a])) 
    {
      word[s] = temp[s];
      
      s++;
      
      temp = '\0';
    }
  }  
  
  
  //for (int s = 0; s < 188; s++) 
  //{
  //  cout << store[s] << endl;  
  //}
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Christian
  • 1
  • 1

1 Answers1

1

Your function's declaration does not match its definition:

void changepassage (string,string&);
void changepassage (string passage,string word) 

See the difference? In the declaration, the 2nd parameter is taking a string by reference, but in the definition it is taking a string by value instead. You need to fix the definition to also pass by reference.

And then, in main(), the word variable being passed in to that 2nd parameter does not match the declaration of that parameter, which is why you are getting a "no matching function" error. word is a string[200] array, not a single string (or a string& reference):

string word[200];
changepassage(passage,word);

You need to drop the [200]:

string word;
changepassage(passage,word);

Also, inside the function itself, you have a store variable:

int plength = passage.length();
string store[plength];

Aside from the fact that such array syntax is non-standard, you probably don't actually want an array holding plength number of strings, but rather a single string whose length can hold plength number of characters. But even so, you are not actually saving anything into store, so you should just get rid of it altogether.

You are also writing chars to word when it does not have space to hold them. You are not pre-sizing the word ahead of time, so use of the [] operator is undefined behavior. You should use the string's += operator or push_back() method instead:

word += temp[s];
word.push_back(temp[s]);

But, to be honest, I don't really know what your function is trying to do. It is a jumbled mess of nonsense and should be rewritten from scratch.


UPDATE: based on a comment you added:

I'm trying to assign each word in the passage to its own element in an array

You need to re-write this function. Try something more like this:

#include <iostream>
#include <string>
using namespace std;

size_t changepassage (const string&, string(&)[200]);

int main() 
{
  const string passage = "...";
  string words[200];
  
  size_t count = changepassage(passage, words);
  for (size_t s = 0; s < count; ++s)
  {
    cout << words[s] << endl;  
  }
}

size_t changepassage (const string& passage, string (&words)[200]) 
{
  if (passage.empty())
    return 0;

  string temp;
  size_t s = 0;
  string::size_type plength = passage.size();

  for (string::size_type a = 0; a < plength; ++a)
  {
    if (isalpha(passage[a]) || (passage[a] != ' ' && passage[a] != '.' && passage[a] != ',' && passage[a] != ';'))
    {
      temp += passage[a];
    }
    else if (ispunct(passage[a]) || isspace(passage[a])) 
    {
      words[s] = temp;
      if (++s == 200) return s;
      temp = "";
    }
  }  
  
  if (!temp.empty() && s < 200)
  {
    words[s] = temp;
    ++s;
  }

  return s;
}

Or:

#include <iostream>
#include <string>
using namespace std;

size_t changepassage (const string&, string*, size_t);

int main() 
{
  const string passage = "...";
  string words[200];
  
  size_t count = changepassage(passage, words, 200);
  for (size_t s = 0; s < count; ++s)
  {
    cout << words[s] << endl;  
  }
}

size_t changepassage (const string& passage, string *words, size_t max_words) 
{
  if (passage.empty() || !words || max_words == 0)
    return 0;

  string temp;
  size_t s = 0;
  string::size_type plength = passage.size();

  for (string::size_type a = 0; a < plength; ++a)
  {
    if (isalpha(passage[a]) || (passage[a] != ' ' && passage[a] != '.' && passage[a] != ',' && passage[a] != ';'))
    {
      temp += passage[a];
    }
    else if (ispunct(passage[a]) || isspace(passage[a])) 
    {
      words[s] = temp;
      if (++s == max_words) return s;
      temp = "";
    }
  }  
  
  if (!temp.empty() && s < max_words)
  {
    words[s] = temp;
    ++s;
  }

  return s;
}

Or, if your instructor will allow it, use std::vector instead:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

void changepassage (const string&, vector<string>&);

int main() 
{
  const string passage = "...";
  vector<string> words;
  
  changepassage(passage, words);

  for (const auto& s : words) 
  {
    cout << s << endl;
  }
}

void changepassage (const string& passage, vector<string>& words)
{
  string temp;  
  string::size_type plength = passage.size();

  for (int a = 0; a < plength; ++a)
  {
    if (isalpha(passage[a]) || (passage[a] != ' ' && passage[a] != '.' && passage[a] != ',' && passage[a] != ';')) 
    {
      temp += passage[a];
    }
    else if (ispunct(passage[a]) || isspace(passage[a]))
    {
      words.push_back(temp);
      temp = "";
    }
  }  

  if (!temp.empty())
    words.push_back(temp);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Ok thank you. My instructor suggested I use the [200] so I assumed it needed to stay in. Though I'm trying to assign each word in the passage to its own element in an array but I'm thinking of just starting from scratch because before I was outputting the words in their own elements but they were stuck in the function and now (after the changes) it's printing out the diamonds with a ? symbol. – Christian Dec 03 '20 at 22:48
  • "*My instructor suggested I use the [200]*" - Then either your instructor gave you bad advice, or you misunderstood what was actually being told to you. "*I'm trying to assign each word in the passage to its own element in an array*" - that would have been good info to provide up front, you did not make that clear enough in your original question. That is not what this code is actually doing, in which case, yes the code definately needs some rewriting to accomplish that properly. – Remy Lebeau Dec 03 '20 at 22:51