PDA

View Full Version : C++ private string in class



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.

geirha
June 29th, 2009, 05:44 PM
You are declaring a new variable called path in get_path. If you want to assign s to the member variable path, then lose the string keyword.

void Directory::get_path(const char* s)
{
path = s;
cout << path << endl;
}

dwhitney67
June 29th, 2009, 06:52 PM
A better method name would be set_path(), instead of get_path().

swappo1
June 29th, 2009, 07:40 PM
When I get rid of string I get the following error.

g++ -O2 -Wall -Werror -ansi -c -o main.o main.cpp
In file included from main.cpp:3:
directory.h:7: error: ‘string’ does not name a type
make: *** [main.o] Error 1

geirha
June 29th, 2009, 07:48 PM
When I get rid of string I get the following error.

Ah, right, you haven't included any headers in your header file, and you should also add the namespace. So in short, #include <string> and std::string path;

MadCow108
June 29th, 2009, 07:50 PM
use std::string in Directory.h

swappo1
June 29th, 2009, 07:52 PM
Thanks. Its been awhile since using header files.