Sign in to follow this  

error C2664: 'std::basic_istream_Elem,_Traits::_M

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can't really figure out what's wrong. It worked FINE when it was inside my main function, but I decided to move it to a different function for ease of use. I decided to make a Yahtzee console game for fun, and to see if I could do it(I have my Associates degree in Software Engineering and going back to college right now for my Bachelors Degree in Game Software Development) I'll just post my whole main.cpp code so you can look, even though I know it's only a couple lines:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include "Dice.h"
#include "Player.h"

using namespace std;

void controlBuffer() {
	//Keep program from going into a loop if invalid data is entered
	fflush(stdin);
	cin.clear();
}

Player* newGame() {
	int numOfPlayers; //Number of human players
	Player *temp_Players; //Player object for players
	string tempPlayerName; //Temporary variable for naming players

	//Get number of human players from user
	do {
		controlBuffer();
		cout<<"Please enter the number of human players: ";
		cin>>numOfPlayers;
		if(cin.fail()) {
			cout<<"Error inputting value, please reenter."<<endl;
		}
	}
	while(cin.fail());

	//Create an array of Players
	temp_Players = new Player[numOfPlayers];

	//Name the players
	for(int i = 0; i < numOfPlayers; i++) {
		cout<<"Enter name for Player "<<(i + 1)<<": ";
		cin.getline(cin, tempPlayerName);
		if(cin.fail()) {
			do {
				//Repeat until name is valid
				controlBuffer();
				cout<<"Invalid name, please reenter: ";
				cin.getline(cin, tempPlayerName);
			}
			while(cin.fail());
		}
		temp_Players[i].setName(tempPlayerName);
	}

	return temp_Players;
}

void main() {
	Player *p_Players; //Player object for players
	string command; //For storing typed commands
	int menuChoice; //Menu choices

	cout<<"----Menu----"
		"/n(1) Start New Game"
		"/n(2) Exit";

	//Repeat until a valid choice is entered
	do {
		cin>>menuChoice;
		if(cin.fail() || menuChoice < 0 || menuChoice > 2) {
			cout<<"Invalid choice, please reenter."<<endl;
		}
	}
	while(cin.fail() || menuChoice < 0 || menuChoice > 2);

	switch(menuChoice) {
		case 1:
			p_Players = newGame();
			break;
		case 2:
			return;
			break;
		default:
			break;
	}

	//Clear memory
	delete[] p_Players;
}


It is the getlines that its whining about: cin.getline(cin, tempPlayerName); but they worked fine inside my main faction, so not really sure what's going on. Thanks for any help.

Share this post


Link to post
Share on other sites
error C2664: 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::istream' to 'char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

Is the full error, and I edited my post: the line the errors occur on is cin.getline(cin, tempPlayerName);

Share this post


Link to post
Share on other sites
Quote:
Original post by Nanven
Is the full error, and I edited my post: the line the errors occur on is cin.getline(cin, tempPlayerName);


The getline istream member function works with a char array as target variable. You probably intended to use the non-member getline function that is found in the string header [smile]

Share this post


Link to post
Share on other sites
Whoa, I didn't even realize I forgot to include <string>!

But... why would it work inside of the main function as intended though?

EDIT: Well I included string, and still getting the same error. How can I forcefully get it to call the right function?

On a side note, i've used getline many times and never had any problems. But always within my main function, so not sure whats so different in main than in a custom function.

Share this post


Link to post
Share on other sites
Thanks for all your help. Wish I could give you a higher rating, but with my default rating I can't really effect you :P Even though I rated every reply.

Share this post


Link to post
Share on other sites

This topic is 4867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this