Accessing private string

This is a discussion on Accessing private string within the C++ Programming forums, part of the General Programming Boards category; Hello, I wan't to get a private string from class CWDirectory in member function set_path(const char* s) and put it ...

  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 12:22 PM. Reason: wrong error message.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,185
    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, 05:05 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. Replies: 8
    Last Post: 04-25-2008, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21