Problem with passing a string to be used as a function parameter.

I want to pass a name of a file in constructor function of a class. Th

Page 1 of 1

4 Replies - 2828 Views - Last Post: 16 February 2007 - 09:51 AM Rate Topic: -----

#1 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Problem with passing a string to be used as a function parameter.

Posted 13 February 2007 - 06:31 AM

#include <iostream>
#include <fstream>
#include <string>
using namespace std; 

class text 
{
private:
	string x;
public:
	 text();
	 text(string nameoffile);
	void contents();
};
text::text()
{}
text::text(string nameoffile)
{
	ifstream filem;
	filem.open(nameoffile);
	while (! filem.eof() )
	{
	  getline (filem,x);
	}
}

void text::contents()
{
	cout << x;
}

void main()
{
	string nameoffile;
	cout << "Enter name of a file:";
	cin >> nameoffile;
	text obj(nameoffile);
	obj.contents();
	system("pause");

}

I want to pass a name of a file in constructor function of a class. That string to be used to open a file and be read from.


Compiled on Microsoft Visiual Studio 8

Quote

1>------ Build started: Project: Chapter7.1, Configuration: Debug Win32 ------
1>Compiling...
1>source.cpp
1>q:\ownwork\chapter7.1\chapter7.1\source.cpp(20) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const wchar_t *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://q:\OwnWork\Chapter7.1\Chapter7.1\Debug\BuildLog.htm"
1>Chapter7.1 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with passing a string to be used as a function parameter.

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Problem with passing a string to be used as a function parameter.

Posted 13 February 2007 - 06:36 AM

Try
filem.open(nameoffile.c_str());


Was This Post Helpful? 0
  • +
  • -

#3 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: Problem with passing a string to be used as a function parameter.

Posted 13 February 2007 - 06:40 AM

View PostAmadeus, on 13 Feb, 2007 - 06:36 AM, said:

Try
filem.open(nameoffile.c_str());



THANK YOU! How does it work? I do not get it.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Problem with passing a string to be used as a function parameter.

Posted 13 February 2007 - 07:12 AM

The .open() method expects a char pointer, or c-style string, as a parameter. You were initially trying to pass a string object instead. The .c_str() method converts a C++ string object into a c style string.
Was This Post Helpful? 0
  • +
  • -

#5 Zammy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-October 06

Re: Problem with passing a string to be used as a function parameter.

Posted 16 February 2007 - 09:51 AM

KUDOS! Thank you for your explanation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1