Thread: Accessing private string

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    Accessing private string

    Hello,

    I wan't to get a private string from class CWDirectory in member function set_path(const char* s) and put it in a member function set_path() in class Directory.
    Code:
    //cwdirectory.cpp
    //class to output the path of the current working directory
    #include <iostream>
    #include <stdexcept>
    #include "cwdirectory.h"
    #include "unistd.h"
    
    #define MAXPATHLEN 200
    
    using namespace std;
    
    CWDirectory::CWDirectory()
    {
    	char buf[MAXPATHLEN];
    	if(getcwd(buf, MAXPATHLEN))
    		set_path(buf);
    	else
    		throw domain_error("pathway not found");
    }
    
    string CWDirectory::set_path(const char* s)
    {
    	return path = s;
    }
    
    void CWDirectory::get_cwdir()
    {
    	cout << path << endl;
    }
    
    .............................................................
    
    //directory.cpp
    //class Directory to output the contents of the current
    //working directory
    #include <iostream>
    #include <stdexcept>
    #include "cwdirectory.h"
    #include "directory.h"
    
    Directory::Directory()
    {
    	set_path();
    }
    
    void Directory::set_path()//function not complete
    {
    	CWDirectory cwdir;
    	cwdpath = cwdir.set_path(buf);//buf causes compiler error
    	cout << cwdpath << endl;
    }
    Here is the error I am getting:
    [hopchewer@hopchewer linux_com]$ make
    g++ -O2 -Wall -Werror -ansi -c -o cwdirectory.o cwdirectory.cpp
    g++ -O2 -Wall -Werror -ansi -c -o directory.o directory.cpp
    directory.cpp: In member function ‘void Directory::set_path()’:
    directory.cpp:17: error: ‘buf’ was not declared in this scope
    make: *** [directory.o] Error 1
    I am not sure how to get the path from class CWDirectory into class Directory to list the contents of the current working directory.
    Last edited by swappo; 06-30-2009 at 11:22 AM. Reason: wrong error message.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I fully agree with the compiler. path is not declared -- notice that path is used in the function set_path(). Is it supposed to be defined as part of class CWDirectory?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    I just edited this post. Some how I had the wrong error. It is buf that is not declared in this scope.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay. buf isn't declared in that function either.

    Do you want the buf variable belonging to cwdir? If so, then use cwdir.buf. Although that seems a bit circular, since buf would just have garbage in it so far as I can tell.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM