2 Replies - 571 Views - Last Post: 29 March 2012 - 06:04 AM Rate Topic: -----

#1 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 170
  • View blog
  • Posts: 608
  • Joined: 12-October 09

no match for operator<<

Posted 29 March 2012 - 05:56 AM

Hello,

I'm trying to teach myself some C++, but i;'m stuck already.

Basically I'm just trying to get some user input and then display it back out again, whilst appending an "!" to the end of it.

#include <iostream>
using namespace std;

string method(string);

int main()
{
	
	cout << "Value: " << method("!") << endl;

	return 0;
}

// Getting user input
string method(string x)
{
	string val;
	
	cout << "Please enter a value..." << endl;
	cin >> val;
	val = val << x;
	
	return val;
}




However, i get: "no match for 'operator<<' in 'val << x'".

Is that not how to concatenate strings? Where am I going wrong?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: no match for operator<<

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: no match for operator<<

Posted 29 March 2012 - 06:01 AM

The << operator is for input streams, what you want is the + operator. That's the nice thing about the string class, you can concatenate two strings just by "adding" them together.
string s = "Hello ";
s = s + " World";


Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: no match for operator<<

Posted 29 March 2012 - 06:04 AM

Quote

Is that not how to concatenate strings? Where am I going wrong?

No, to concatenate std::strings use the operator+, operator +=, etc. See this link for some documentation on the std::string class.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1