I have setup a class that stores the data for my database connections. Inside the class I have several members that either set or get the variables.

Inside my main I have set the value of the hostname. Now I have a second class that I am testing with called TestClass. What I am trying to do is access the value that the main method set inside of my TestClass.

Here is the current code I am testing with.

I am not an expert in c++, and i am trying to better educate myself. I took the over all concept from something I would use in as3.

Any help with this would be greatly appreciated.

mainTest.cpp

int main()
{
  Config cfg;
  DbConfig dbcfg;
  TestClass tclss;

  try
  {
    /* Load the configuration.. */
    cout << "loading [config.cfg]..";
    cfg.readFile("config.cfg");
    cout << "ok" << endl;

    	string hostname = cfg.lookup("application.live.hostname");
        dbcfg.setDatasource(hostname.c_str());

        cout << dbcfg.getDatasource() << endl;
        tclss.testDSource();



    cout << "Done!" << endl;
  }
  catch (...)
  {
    cout << "failed" << endl;
  }

  return 0;
}

DbConfig.cpp

#include "DbConfig.h"
#include <string>
#include <iostream>



	DbConfig::DbConfig(){}

	void DbConfig::setDatasource(const char* dSource){
		datasource = dSource;
	}

	const char* DbConfig::getDatasource(void){
		return (datasource);
	}

TestClass.cpp

TestClass::TestClass() {}


void TestClass::testDSource(){

	DbConfig dbcfg;

	std::cout << "DATASOURCE : " << dbcfg.getDatasource() << std::endl;


}

The real issues are all in #include "DbConfig.h", however, without any includes in TestClass, it will not compile.

Can you post DbConfig.h and we can have a look and maybe solve your problem. (also TestClass.h if it exists)

Here is the full source code of my test project.

I understand part of the problem is that I am reinstanciating DbConfig in test class. I am just not sure how to instanciate DbConfig in a way that is is accessible to other classes.

TestClass.h

#ifndef TESTCLASS_H_
#define TESTCLASS_H_
#include "DbConfig.h"

class TestClass {
public:
	TestClass();

	void testDSource();
};

#endif /* TESTCLASS_H_ */

TestClass.cpp

#include <iostream>
#include "TestClass.h"
#include "DbConfig.h"

TestClass::TestClass() {}


void TestClass::testDSource(){
       DbConfig dbcfg;
	std::cout << "DATASOURCE : " << dbcfg.getDatasource() << std::endl;

}

DbConfig.h

#ifndef DBCONFIG_H_
#define DBCONFIG_H_
#include <string>

class DbConfig {
	const char* datasource;
	const char* hostname;
	const char* username;
	const char* password;
public:
	DbConfig();

	void setDatasource(const char* dSource);					// set the datasource name

	const char* getDatasource(void);								// get the datasource name

	void setHostname(const char*  hName);						// set the hostname

	const char* getHostname(void);								// get the hostname

	void setUsername(const char*  uName);						// set the username

	const char* getUsername(void);								// get the username

	void setPassword(const char*  pWord);							// set the password

	const char* getPassword(void);								// get the password


};

#endif /* DBCONFIG_H_ */

DbConfig.cpp

#include "DbConfig.h"
#include <string>
#include <iostream>



	DbConfig::DbConfig(){}

	void DbConfig::setDatasource(const char* dSource){
		datasource = dSource;
	}

	const char* DbConfig::getDatasource(void){
		return (datasource);
	}

	void DbConfig::setHostname(const char* hName){
		hostname = hName;
	}

	const char* DbConfig::getHostname(void){
		return (hostname);
	}

	void DbConfig::setUsername(const char* uName){
		username = uName;
	}

	const char*DbConfig::getUsername(void){
		return (username);
	}

	void DbConfig::setPassword(const char* pWord){
		password = pWord;
	}

	const char* DbConfig::getPassword(void){
		return (password);
	}

mainTest.cpp

#include <iostream>
#include <libconfig.h++>
#include <string>
#include "DbConfig.h"
#include "TestClass.h"

using namespace libconfig;
using namespace std;

/***************************************************************************/

int main()
{
  Config cfg;
  DbConfig dbcfg;
  TestClass tclss;

  try
  {
    /* Load the configuration.. */
    cout << "loading [config.cfg]..";
    cfg.readFile("config.cfg");
    cout << "ok" << endl;

    	string hostname = cfg.lookup("application.live.hostname");
        dbcfg.setDatasource(hostname.c_str());


        cout << dbcfg.getDatasource() << endl;
        tclss.testDSource();



    cout << "Done!" << endl;
  }
  catch (...)
  {
    cout << "failed" << endl;
  }

  return 0;
}

Ok I am going to write some very short code that shows you your main error. [There may be others but this MUST be fixed]

Let us start with const char* . It is good that you are thinking about const BUT you can't randomly assign

class A
{
  const char* X;
public:
  void setC(const char* V)
    { X=V; }
  const char* getC() const {return X;}
};

int
main()
{
  std::string X="fred";
  A obj;
  obj.setC(X.c_str());
  std::cout<<"G == "<<obj.getC()<<std::endl;
  X="John";
  std::cout<<"G == "<<obj.getC()<<std::endl;
}

Note that this print Fred the John.
That is because you are using the pointer to point to the address of your string, then it changes and then you are in a mess. Particularly, when it is deleted because it goes out of scope.

So in short, why don't you use std::string in DbConfig ?
e.g.

class DbConfig {

  std::string datasource;
  std::string hostname;
  std::string username;
  std::string password;
public:
   void setDataSource(const std::string& A)
        { datasource=A; }
// the rest here...
};

If you want to use char* then you should use a copy command, e.g.
strncpy/strcpy or your own loop.

Ok that made a little more sense to me coming from an as3 background.
However I am still stuck on my original problem, of accessing the stored value of lets say datasource when trying to access it from a different class file.

Let say I have this code. Using the same classes from above. How would I access the class that was istanciated in main. So that I have access to the values of the variables set in main in other class files.

I guess the whole idea would be make the values stored in DbConfig "globally" accessible to other classes. I use the term globally loosely as everything I have read frowns upon using global vars and objects.

#include <string>
#include "DbConfig.h"
#include "TestClass.h"


using namespace std;


int main(){
	DbConfig dbcfg;
	TestClass tclss;
	
	
	dbcfg.setDatasource("test");
	
	cout << tclss.testDSource() << endl;	
}

think it may have to do with tclss.testDSource(), could you provide the errors you are receiving from the compiler please?

I do not get any errors when compiling. The problem is, the value that I set in main is test. With the code below, if I were to call tclss.testDSource() from main I should get back DATASOURCE : test. But instead I just get back DATASOURCE :

As I posted earlier in my thread, I know what the issue is. The issue is, I am re-instantiating DbConfig in TestClass. I know this is incorrect. What I need to do is gain access to the already instantiated DbConfig object that was instantiated in main.

#include <iostream>
#include "TestClass.h"
#include "DbConfig.h"

TestClass::TestClass() {}


void TestClass::testDSource(){
	DbConfig dbcfg;

	std::cout << "DATASOURCE : " << dbcfg.getDatasource() << std::endl;

}

TestClass has no means to find the DbConfig declared inside main.

If TestClass needs to access it, that DbConfig must either be given to TestClass as a parameter of some sort or if having one and only one DbConfig would be proper, you could provide support for and access to the 'global' DbConfig via a singleton implementation.

The DbConfig declared inside TestClass::testDSource() is unrelated (as it applies to data) from the one in main().

thanks this is very interesting question here

Your problem is that testClass has a single function that creates a new instance of DbConfig, it has NOTHING to do with the version in int main() , which is dbcfg . Then you try to write out an UNINITIALIZED string.

You would have found that out if you have used initializers for all your private variables in the default constructor.

In short: testClass, should store a reference/pointer to a DbConfig, so that it can work on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.