title: C++ - '(const char*)' not declared in scope?
body: <p>I want to make a custom String class for C++. But when I do this:</p>
  
  <pre><code>g++ test.cpp sys/Base.h sys/Base.cpp
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>sys/Base.cpp: In function 'const char* Base::toChar()':
  sys/Base.cpp:57:13: error: 'strval' was not declared in this scope
        return strval;
               ^
  sys/Base.cpp: In function 'std::string Base::toStr()':
  sys/Base.cpp:60:20: error: 'strval' was not declared in this scope
        return string(strval);
                      ^
  </code></pre>
  
  <p>test.cpp</p>
  
  <pre><code>#include "sys/Base.h"
  int main() {
      Base::write("Hello there.\n");
      return 0;
  }
  </code></pre>
  
  <p>sys/Base.h</p>
  
  <pre><code>// Header file handling
  #ifndef ARAVK_BASE_H
  #define ARAVK_BASE_H
  
  // Includes
  #include &lt;string&gt;
  
  // Global variables
  #define EXIT_YAY 0
  #define EXIT_ERR 1
  
  using namespace std;
  
  namespace Base {
      // Classes:
          class String {
                  static const char* strval;
              public:
                  // Constructors:
                      String();
                      String(char[]);
                      String(const char*);
                      String(string);
                  // Destructors:
                      ~String();
  
                  // Operators:
                      // =
                          void operator=(const String&amp;);
                          void operator=(const char*&amp;);
                          void operator=(const string&amp;);
  
                  // Conversion:
                      const char* toChar() const;
                      string toStr() const;
          };
      // Functions:
          // Input-Output:
              // Write:
                  void write(String);
                  void write(string);
                  void write(const char*);
              // Read:
                  String read();
  
          // Executing:
              String run(String);
  }
  #endif
  </code></pre>
  
  <p>sys/Base.cpp</p>
  
  <pre><code>// Including
  #include "Base.h"
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;cstdio&gt;
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include &lt;stdexcept&gt;
  
  // Global variables
  #define EXIT_ERR 1
  #define EXIT_YAY 0
  
  /* ------------------------ */
  using namespace std;
  
  namespace Base {
      // Classes
          // String functions
              // Constructors
                  String::String() {
                      const char* strval = "";
                  }
                  String::String(const char* str) {
                      const char* strval = str;
                  }
                  String::String(string str) {
                      const char* strval = str.c_str();
                  }
                  String::String(char str[]) {
                      const char* strval = str;
                  }
              // Destructors
                  String::~String() {
                      delete strval;
                  }
              // Operators
                  // =
                      void String::operator=(const String &amp;strp) {
                          strval = strp.toChar();
                      }
                      void String::operator=(const char* &amp;strp) {
                          strval = strp;
                      }
                      void String::operator=(const string &amp;strp) {
                          strval = strp.c_str();
                      }
              // Conversion:
                  const char* toChar() {
                      return strval;
                  }
                  string toStr() {
                      return string(strval);
                  }
  
      // Functions:
          // Input-Output:
              // Write
                  void write(String str)        { printf(str.toChar()); }
                  void write(const char* str) { printf(str);              }
                  void write(string str)        { printf(str.c_str());  }
              // Read
                  String read()                       { char str[100]; scanf("%s", str); return String(str); }
                  //TODO: More to come
  
          // Executing
              /*String run(String command) {
                  const char* cmd = command.toChar();
                  char buffer[128];
                  string result = "";
                  std::shared_ptr&lt;FILE&gt; pipe(popen(cmd, "r"), pclose);
                  if (!pipe) throw runtime_error("popen() failed!");
                  while (!feof(pipe.get())) {
                      if (fgets(buffer, 128, pipe.get()) != NULL)
                          result += buffer;
                  }
                  return String(result);
              }*/
              String run(String command) {
                  char buffer[128];
                  std::string result = "";
                  const char* cmd = command.toChar();
                  FILE* pipe = popen(cmd, "r");
                  if (!pipe) throw std::runtime_error("popen() failed!");
                  try {
                          while (!feof(pipe)) {
                                  if (fgets(buffer, 128, pipe) != NULL)
                                          result += buffer;
                          }
                  } catch (...) {
                          pclose(pipe);
                          throw;
                  }
                  pclose(pipe);
                  return String(result);
              }
  
  }  
  </code></pre>
  
  <p>I'm not sure why this is happening. I think it's related to how I've declared/defined the const char* 'strval'. Can anybody help?
  P.S: If the answer is too big, this project is on Github: <a href="http://github.com/AravK/C-Applications" rel="nofollow">AravK/C-Applications</a></p>