0
votes

My program was working fine with the main function at the end, but when I added in the function prototyping and put the main function in the beginning I started to get these errors.

error C2664: 'getData' : cannot convert parameter 1 from 'std::string [100]' to 'std::string' No constructor could take the source type, or constructor overload resolution was ambiguous

error C2664: 'descending' : cannot convert parameter 1 from 'std::string [100]' to 'std::string' No constructor could take the source type, or constructor overload resolution was ambiguous

error C2664: 'ascending' : cannot convert parameter 1 from 'std::string [100]' to 'std::string' No constructor could take the source type, or constructor overload resolution was ambiguous

error C2664: 'output' : cannot convert parameter 1 from 'std::string [100]' to 'std::string'

No constructor could take the source type, or constructor overload resolution was ambiguous

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

void swap(string&, string&);
void getData(string, int&);
void ascending(string, int);
void descending(string, int);
void output(string, int);
int getChoice();

int main()
{
   string name[100], temp;
   int item, choice;

   getData(name, item);

   choice = getChoice();

   if (choice == 1)
      ascending(name, item);
   if (choice == 2)
      descending(name, item);

   output(name, item);
 }



void swap(string& name1, string& name2)
{
   string temp;
   temp  = name1;
   name1 = name2;
   name2 = temp;
}


void getData(string name[], int& item)
{
   ifstream  fin;
   fin.open("E:\\p9.txt");

   item = 0;

   while (!fin.eof())
   {
      fin >> name[item];
      item++;    
   }
   cout << "item = " << item << endl << endl;
}

void ascending(string name[], int item)
{
   for(int j=0; j<item-1; j++)
   {
      for(int i=0; i<item-1; i++)
         if(name[i] > name[i+1])
            swap(name[i], name[i+1]);
   }
}

void descending(string name[], int item)
{
   string temp;
   for(int j=0; j<item-1; j++)
   {
      for(int i=0; i<item-1; i++)
         if(name[i] < name[i+1])
            swap(name[i], name[i+1]);
   }
}




void output(string name[], int item)
{
   for(int i=0; i<item; i++)
      cout << name[i] << endl;
   cout << endl << endl;
}

int getChoice()
{
   int choice;
   cout << "Please enter 1 for ascending or 2 for descending." << endl;
   cin  >>  choice;
   return choice;
}
2
Not error-related, but change while (!fin.eof()) { fin >> name[item]; item++; } to while (fin >> name[item++]);. - chris

2 Answers

3
votes

Well, your "prototypes" don't match your function definitions. Why did you introduce prototypes that have no relation to the actual functions?

Here's one example

void getData(string, int&);

void getData(string name[], int& item) { ... }

The correct "prototype" in this case is

void getData(string[], int&);
0
votes

Name should declared as a string(should not be array):

int main()
{
   string name, temp;
   int item, choice;

   getData(name, item);

   choice = getChoice();
.....