Hi all,

I am trying to write a general function that when called is able to write the value of all the member variables of an object passed to it. This function is part of a class.

The class objects that I want to write out each have a member called "key" which stores the names of the other variables in the class in a vector of strings.

I'm basically trying to write the following type of function to write out the contents of the member variables:

void writeObject2File( string objectName, string outputFilename )
{
    ofstream datafile;
    datafile.open( filename.c_str() );

    for ( unsigned int i=0; i<key.size(); i++ )
        datafile << objectName.c_str()->key.at( i ).c_str();
}

This doesn't work, which I guess is probably quite a novice error. The issue I'm having right now is with the following bit:

objectName.c_str()->key.at( i ).c_str();

How do I use the objectName string which defines the name of the object to access the specific object? And how do I then use the variable names stored in the vector key to access their values and subsequently print them to the datafile?

I would really appreciate it if someone can help me sort this out.

Thanks in advance,

Kartik

Hi all,

I am trying to write a general function that when called is able to write the value of all the member variables of an object passed to it. This function is part of a class.

The class objects that I want to write out each have a member called "key" which stores the names of the other variables in the class in a vector of strings.

I'm basically trying to write the following type of function to write out the contents of the member variables:

void writeObject2File( string objectName, string outputFilename )
{
    ofstream datafile;
    datafile.open( filename.c_str() );

    for ( unsigned int i=0; i<key.size(); i++ )
        datafile << objectName.c_str()->key.at( i ).c_str();
}

This doesn't work, which I guess is probably quite a novice error. The issue I'm having right now is with the following bit:

objectName.c_str()->key.at( i ).c_str();

How do I use the objectName string which defines the name of the object to access the specific object? And how do I then use the variable names stored in the vector key to access their values and subsequently print them to the datafile?

I would really appreciate it if someone can help me sort this out.

Thanks in advance,

Kartik

As far as I can see it, your function takes a std::string as the first parameter. So what is going into the function is not actually an instance of your class, but merely a string representing the name of the class. Perhaps you should alter the function so it takes an instance of your class rather than a string! (or possibly in addition to the string).

So you could perhaps do something like this:

void writeObject2File( const MyClass &myObject, const std::string &outputFilename )
{
    ofstream datafile;
    datafile.open( filename.c_str() );

    for ( unsigned int i=0; i<key.size(); i++ )
        datafile << myObject.name().c_str() << myObject.key.at( i ).c_str();
}

I've used 'MyClass' as the passed in objects type. Obviously you should use whatever class you're using.

Also, I've not compiled or tested any of the code or anything, it is merely a suggestion. The function might not be doing exactly what you want it to either, but it should at least give you a gentle push in the right direction and show you where you were going wrong!
Cheers for now,
Jas.

As far as I can see it, your function takes a std::string as the first parameter. So what is going into the function is not actually an instance of your class, but merely a string representing the name of the class. Perhaps you should alter the function so it takes an instance of your class rather than a string! (or possibly in addition to the string).

So you could perhaps do something like this:

void writeObject2File( const MyClass &myObject, const std::string &outputFilename )
{
    ofstream datafile;
    datafile.open( filename.c_str() );

    for ( unsigned int i=0; i<key.size(); i++ )
        datafile << myObject.name().c_str() << myObject.key.at( i ).c_str();
}

I've used 'MyClass' as the passed in objects type. Obviously you should use whatever class you're using.

Also, I've not compiled or tested any of the code or anything, it is merely a suggestion. The function might not be doing exactly what you want it to either, but it should at least give you a gentle push in the right direction and show you where you were going wrong!
Cheers for now,
Jas.

Hi,

Thanks for your feedback. I had thought about just passing the object directly, but the problem is that I want to write the function in such a fashion that I pass an object of any class. I have objects of several different types of classes that I would like to pass, and by using the variable-name, they are automatically identified without the class being specified literally in the arguments of the function.

I'm not sure whether this is possible in some way in C++.

Anymore input would be greatly appreciated.

Thanks,

Kartik

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.