Skip to main content

Thread: C++ private string in class


hello,

trying figure out how declare string private member of class can use in other member functions of class. here code:
code:
directory.cpp  #include <iostream>  #include <stdexcept>  #include "directory.h"  #include "unistd.h"    #define maxpathlen 200    using namespace std;    directory::directory()  {  	char buf[maxpathlen];  	if(getcwd(buf, maxpathlen))  		get_path(buf);  	else  		throw domain_error("pathway not found");  }    void directory::get_path(const char* s)  {  	string path = s;  	cout << path << endl;  }    directory.h  #ifndef directory_h  #define directory_h    class directory  {  	private:                  string path; //here declare string  	public:  		directory();  		void get_path(const char* s);    };  #endif
if declare string in directory want set buf in constructor function , able use in other member functions. not sure how this.

you declaring new variable called path in get_path. if want assign s member variable path, lose string keyword.
code:
void directory::get_path(const char* s) { 	path = s; 	cout << path << endl; }


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk C++ private string in class


Ubuntu

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums