Using strings as parameters

C++ will not allow it!

Page 1 of 1

9 Replies - 596 Views - Last Post: 09 August 2010 - 01:46 PM Rate Topic: -----

#1 Guest_thegr8n8*


Reputation:

Using strings as parameters

Posted 09 August 2010 - 06:44 AM

How do I use strings in the functions I am creating in c++? ?I know they are not a type, but how can I use them as a type?
Is This A Good Question/Topic? 0

Replies To: Using strings as parameters

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Using strings as parameters

Posted 09 August 2010 - 06:54 AM

strings are a variable type in C++, using as a parameter:

void function(string strName){
    //do something with strName here
}


Was This Post Helpful? 1
  • +
  • -

#3 Guest_thegr8n8*


Reputation:

Re: Using strings as parameters

Posted 09 August 2010 - 06:58 AM

I get the error string as not declared along with an ISO problem
Was This Post Helpful? 0

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3105
  • View blog
  • Posts: 19,144
  • Joined: 14-September 07

Re: Using strings as parameters

Posted 09 August 2010 - 07:16 AM

Are you including the appropriate headers? Code example please.
Was This Post Helpful? 0
  • +
  • -

#5 thegr8n8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-August 10

Re: Using strings as parameters

Posted 09 August 2010 - 07:18 AM

wait a minute, I think its now solved. I have to use namespace std; righT?
Was This Post Helpful? 0
  • +
  • -

#6 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 341
  • Joined: 16-July 10

Re: Using strings as parameters

Posted 09 August 2010 - 07:21 AM

You might wanna post your code here and let us see what's wrong.
This is the simplest way of using string as parameter of a function.


#include <iostream>

using namespace std;

void strFunc(string str){
    cout << str << endl;
}

int main(){

	strFunc("Hehehehe");
	return 0;
}


Was This Post Helpful? 1
  • +
  • -

#7 MathiasVP  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 154
  • Joined: 08-August 10

Re: Using strings as parameters

Posted 09 August 2010 - 07:22 AM

I often get confused by the string headers, don't make the same mistake!

string.h, string and cstring are all different from each other
Was This Post Helpful? 1
  • +
  • -

#8 thegr8n8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-August 10

Re: Using strings as parameters

Posted 09 August 2010 - 07:45 AM

okay I was using the regular string header.
Was This Post Helpful? 0
  • +
  • -

#9 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using strings as parameters

Posted 09 August 2010 - 11:40 AM

Just a comment on Cuzzie's example,

You can declare a type of std::string by only including the <iostream> but you cannot use any of it's functions such as length(), getline(), etc.

Bottom line, if you are using strings include the <string> header not just the <iostream>.

This post has been edited by eker676: 09 August 2010 - 11:40 AM

Was This Post Helpful? 2
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Using strings as parameters

Posted 09 August 2010 - 01:46 PM

the <string> header is not defined in <iostream>. std::string is partially inherited/included into iostream through ios_base, therefor it includes ios_base which will let you compile a instance of std::string without the <string> header.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1