0

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;
}
1
  • Not error-related, but change while (!fin.eof()) { fin >> name[item]; item++; } to while (fin >> name[item++]);. Commented Nov 30, 2012 at 6:05

2 Answers 2

3

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&);
Sign up to request clarification or add additional context in comments.

1 Comment

oh, I got it. Thanks for the help. I didn't realize you had to add the brackets in as well.
0

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

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

   getData(name, item);

   choice = getChoice();
.....

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.