String variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rottmanj
    New Member
    • Jan 2008
    • 46

    String variables

    I am new to oop c++ and I am trying to better educate myself on it. One issue that I have run into is how to store a string based variable.

    Everything I have tried ends up with the errors below. Any information on this would be greatly appreciated.

    Code:
    DbConnect.h:7: error: ‘string’ does not name a type
    DbConnect.h:8: error: ‘string’ has not been declared
    DbConnect.h:9: error: ‘string’ does not name a type
    DbConnect.cpp:5: error: variable or field ‘db_set’ declared void
    DbConnect.cpp:5: error: ‘string’ was not declared in this scope
    DbConnect.cpp:9: error: ‘string’ does not name a type
    Here is my code.

    DbConnect.h
    Code:
    #ifndef DBCONNECT_H_
    #define DBCONNECT_H_
    
    class DbConnect
    {
    public:
    	string keep_value;				// private data value
    	void db_set(string db_value);	// public method to set value
    	string db_get(void);					// public method to get value
    };
    
    #endif /*DBCONNECT_H_*/
    DbConnect.cpp
    Code:
    #include "DbConnect.h"
    #include <string>
    
    
    void DbConnect::db_set(string db_value){
    	keep_value = db_value;
    }
    
    string DbConnect::db_get(void){
    	return (keep_value);
    }
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    This is a namespacing issue. You need to either put "std::strin g" everywhere instead of just string or put "using namespace std" at the top of the file.

    You also need to #include <string> in your .h file (any file that wants to use strings must #include <string> or include a file that does)
    Last edited by mac11; Dec 4 '08, 04:50 PM. Reason: noticed the .h didn't #include <string>

    Comment

    Working...