参数'2'无法将'std :: string'转换为'std :: string *'

时间:2011-12-07 20:53:36

标签: c++

  

可能重复:
  Partially Fill an array from a file

我不知道这意味着什么。我的所有变量排成一行。我似乎无法找到任何错误。

我也收到错误错误:'operator<<'不匹配在'infile<<名 如果你用numoftoys填充名字,那就是它给我的另一个错误。

/***************************************************/
/* Author:     Sam LaManna                         */
/* Course:                                         */
/* Assignment: Program 6 Elves                     */
/* Due Date:   11/22/11                            */
/* Filename:   program6.cpp                        */
/* Purpose:    Write a program that will process   */
/*             the work done by santas elfs        */
/***************************************************/

#include <iostream>     //Basic input/output
#include <iomanip>      //Manipulators
#include <string>       //String stuff 
#include <fstream>      //File input/output

using namespace std;

void instruct ();     //Function Declaration for printing instructions 
void input (ifstream &infile, string names [50], int numoftoys[50], int &i);    //Function declaration for getting data from file

int main()
{

  string names [50] = { };       //Array for storing names
  int numoftoys [50] = { };      //Array for storing the number of toys made
  int i = 0;

  ifstream infile("eleves.dat"); //Opens input file "elves.dat"

  instruct();     //Function call to print instructions

  while (!infile.good())
    {
      input (infile, names[i] , numoftoys[i]);
      ++i;
    }

  cout << names << "\n" << "\n";

  cout << numoftoys << "\n" << "\n";




  return 0;
}




/***************************************************/
/* Name: instruct                                  */
/* Description: Prints instructions to user        */
/* Parameters: N/A                                 */
/* Return Value: N/A                               */
/***************************************************/

void instruct ()                                   
{
  cout << "\n" << "This program will calculate the toys made by santas elfs and assign" << "\n";
  cout << "a rating to each elf. It will also sort them and print average, min and max." << "\n";
  cout << "\n" << "Make sure you have a file named elves.dat in the same directory as";
  cout << "this porgram or you will recieve errors.";
  cout << "\n" << "\n";

  return;
}


/***************************************************/
/* Name: input                                     */
/* Description: Reads from file                    */
/* Parameters: N/A                                 */
/* Return Value: N/A                               */
/***************************************************/

void input (ifstream &infile, string names [50], int numoftoys[50], int &i)
{
  infile << names;
  infile << numoftoys;
  infile.ignore ('\n');

  return;
}

2 个答案:

答案 0 :(得分:3)

因为你声明你运行一个数组,所以退化成一个指向第一个元素的指针。

void input (ifstream &infile, string names [50], int numoftoys[50], int &i);  

在这里,您将一个字符串和一个int传递给input() - 因为names [i] - 是数组中的单个字符串。

input (infile, names[i] , numoftoys[i]);

我建议改变你的功能

void input(ifstream &infile, string &name, int &numoftoy)
{
  infile >> name;
  infile >> numoftoy;
  infile.ignore ('\n');

// return; no need return 
}

答案 1 :(得分:0)

你应该迭代字符串数组。你得到错误的原因,因为只有运算符&lt;(&lt;(ostream&amp; os,const string&amp; str)是defined,而不是运算符&lt;&lt;(ostream&amp; os,const string * str)。

可能的灵魂将使用for语句迭代数组。另一个(更优雅的)解决方案是使用std::for_each