swappo1
June 29th, 2009, 05:16 PM
Hello,
I am trying to figure out how to declare a string as a private member of a class so I can use it in other member functions of that class. Here is my 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 is where I would declare the string
public:
Directory();
void get_path(const char* s);
};
#endif
If I declare the string in Directory I would want to set it to buf in the constructor function and then be able to use it in other member functions. I am not sure how to do this.
I am trying to figure out how to declare a string as a private member of a class so I can use it in other member functions of that class. Here is my 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 is where I would declare the string
public:
Directory();
void get_path(const char* s);
};
#endif
If I declare the string in Directory I would want to set it to buf in the constructor function and then be able to use it in other member functions. I am not sure how to do this.