Can I call a class using a variable?

I want my code to call a class but I need part of the class name to come from a variable. i.e. class tree_treeData. Is this possible?

Basically, I run a form to get a 4 digit number and I want to use that 4 digit number to get specific info from a specific class.

1
2
3
4
5
6
7
8
int main()
{
    int treeForm();
    int treeData=0;
    treeData=treeForm();

    std::cout << treeData << " End of program.";
}


Would I call it like this?
 
string tree_treeData::info


If this was my class

1
2
3
4
5
6
7
class tree_1111
{
public:
    string info = "Tree info goes here"
protected:
private:
};


It's not working so I'm guessing I'm either doing it wrong or I can't do it but I'm not quite sure how to get the class called otherwise.

Thanks,

Autumn
Last edited on
Hmm... "Foo::bar" means that the bar is defined inside namespace Foo.
http://en.cppreference.com/w/cpp/keyword/namespace - for mode details and examples.
And you want to get access to field of object of class tree_1111 so you need to do it with member access operator.
http://en.cppreference.com/w/cpp/language/operator_member_access

So it should be something like this:
1
2
3
4
 
tree_1111 tree_treeData; //create an object of your class with default parameters
string someString = tree_treeData.info; // getting access to it's field
 
Hello agill,

It looks like you my need to read this first"

http://www.cplusplus.com/doc/tutorial/classes/

Follower at some time by:

http://www.cplusplus.com/doc/tutorial/templates/

The first thing you will have to do is create an object or instance of the class in main. Based on what I see it might be: tree_treeData treeData; or any other name you would like. Then in the program you would call a member function of the class as treeDats.info;.

You will have to use public member functions of the class to access private member variables of the function. There is no way to directly access the private member variables or functions outside the class.

I do not know if it is possible to create a string to call a function or not, but it sounds like fun to give it a try.

In the first bit of code it looks: treeData=treeForm(); is calling a non class function that returns a value or did you mean to get information from a class?

The class you have shown is not the best way to write the class. Something like this is a better use of the class:
1
2
3
4
5
6
7
8
9
10
11
class tree_1111
{
	public:
                tree_1111()  // <--- Default ctor.
                tree_1111(std::string str); //<--- Overloaded ctor to set string when defined in main.
		std::string GetInfo();  // <--- Used to get the private data.
	private:
                std::string	string info;
};

std::string tree_1111:GetInfo() { return info; }


What you have makes the variable "info" public which defeats the point of the class. A struct or simple variable would work just as well.

Thinking about your question I do not believe that using a string to create a variable name for the class will work. I am thinking that it would give a redefinition error of the string name at compile time.

What might work better is to use one object of the class and store that information in a vector or array then change the data before storing the changes in the vector or array.

This each element of the vector or array would contain different data values.

Hope that helps,

Andy
Thanks, that helps clarify things a lot. I'll go over those links as well and try your idea of using vectors/arrays. Have a good day!
I seem to still be having an issue calling the class normally and I'm not sure why. Could you take a look? I'm sorry to bother, I've read both the links you posted and have been at it now for a few hours and the only error code I'm getting says:

||=== Build: Debug in FP_TreeID (compiler: GNU GCC Compiler) ===|
|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|
|error: no 'std::__cxx11::string tree_1111::GetInfo()' member function declared in class 'tree_1111'|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|


the .h that is receiving the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef TREEDATA_H
#define TREEDATA_H


class treedata
{
    public:
        treedata();
        virtual ~treedata();
    protected:
    private:
};

class tree_1111
{
	public:
                tree_1111();  // <--- Default ctor.
                tree_1111(std::string str); // Overloaded ctor to set string when defined in main.
                std::string getInfo(); // Used to get the private data.
	private:
                std::string info = "Tree info goes here";
};

std::string tree_1111::GetInfo() { return info; }  //This is the line receiving the errors

#endif // TREEDATA_H



My main code setting the tree_1111 class to treeInfo and trying to call it using tree.Info.getInfo();

1
2
3
4
5
6
7
8
9
10
11
12
#include "src/treedata.cpp"

int main()
{
    tree_1111 treeInfo;
    int treeForm();
    int treeData=0;
    treeData=treeForm();

    treeInfo.getInfo();
    std::cout << treeData << " End of program.";
}


Thanks!!
Last edited on
Hello agill,

Line 19 std::string getInfo();.

Line 24 std::string tree_1111::GetInfo().

Notice the difference and does the third line of the error messages make more sense now.

I am not as familiar with your compiler, but you may need to add "-std=c++11" to the command line arguments when compiling the program.

Hope that helps,

Andy
Topic archived. No new replies allowed.