calling methods from another class

I have two classes Node and element and I made a vector of each class's objects;

1
2
3
4
5
6
7
8
9
10
class Node {
public:

	VecDbl_t* Coordinates_;
	Node() = default;
	Node(VecDbl_t* coord);

	double get_x();
	double get_y();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#endif

#include "node.h"

Node::Node(VecDbl_t* coord) {
	Coordinates_ = coord;
}

double Node::get_x() {
	return (*Coordinates_)[0];
}
double Node::get_y() {
	return (*Coordinates_)[1];
}


1
2
3
4
5
6
				VecNode_t Nodes;
				for (size_t i_node = 0; i_node < Nnodes; i_node++) {
					VecDbl_t coord = Global_Mesh.Coordinates_[i_node];
					Node node(&coord);
					Nodes.push_back(node);
				}


and I have a similar class for elements;

now in another class I pass the vectors of node and elements objects by reference into constructor and I need to use the constructor and methods of Node and Element class inside the third one. I made the third class as children of these two classes but still I am not able to do that. any help would be appreciated.



Last edited on
I made the third class as children of these two classes but still I am not able to do that. any help would be appreciated.
Can you show an example please. It's not clear to me.
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
28
29
#include "element.h"
#include "node.h"
#include "sparcematrixstruct.h"

class Assembler:public Element, public Node {

public:

	VecIdx_t *connec;
	VecDbl_t *coord;
	VecElm_t Elements;
	VecNode_t Nodes;
	VecVecDbl_t NN_;
	VecVecDbl_t NNx_;
	VecVecDbl_t NxN_;
	VecVecDbl_t NNy_;
	VecVecDbl_t NyN_;
	VecVecDbl_t NxNx_;
	VecVecDbl_t NyNy_;
	VecIdx_t *row_ptr_;
	VecIdx_t *col_ind_;

	Assembler() = default;
	Assembler(VecNode_t &Nodes, VecElm_t &Elements);

};


#endif 


1
2
3
4
5
6
7
8
9
Assembler::Assembler(VecNode_t &Nodes, VecElm_t &Elements){

	for (int i = 0; i < Elements.size(); i++)
	{
		Elements[i]./*after this dot I need to see the memebr function of other class but nothing*/
	}


}
Last edited on
What is VecElm_t? A std::vector<Something>?
If yes, the what public interface does Something have?
@Keskiverto what do you mean by public interface?
In this case, the public members of the class (assuming that Something is a class).
If it doesn't have a operator[], then you can't do that, but it problably does because VecDbl_t does.

We can't say for sure because:
* you haven't shown us the definition of that VecElm_t
* you haven't shown the actual error the compiler generated
This is my element class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Element::Element(VecIdx_t* connec, VecNode_t& Nodes) {
	VecVecDbl_t tmp_coordinates;
	Connect_ = connec;
	size_t n_nodes = Connect_->size();
	tmp_coordinates.resize(n_nodes);

	for (size_t i = 0; i < n_nodes; i++) {
		size_t i_node = (*Connect_)[i];
		tmp_coordinates[i].push_back(Nodes[i_node].get_x());
		tmp_coordinates[i].push_back(Nodes[i_node].get_y());
	}
	Calculate_ElmSize_(tmp_coordinates);

}

void Element::set_vertices(VecIdx_t* connec)
{
	Connect_ = connec;
	Node0 = (*Connect_)[0];
	Node1 = (*Connect_)[1];
	Node2 = (*Connect_)[2];
}


and this is
VecElm_t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef std::vector<Element>	VecElm_t; 		
VecElm_t Elements;
		for (size_t i_elm = 0; i_elm < Local_Mesh.Nelms_; i_elm++) {
			VecIdx_t connect = Local_Mesh.Connectivities_[i_elm];
			Element elm(&connect, Nodes);
			Elements.push_back(elm);
			Elements[i_elm].set_vertices(&connect);
			Elements[i_elm].Node0;
			Elements[i_elm].Node1;
			Elements[i_elm].Node2;

			std::cout << Elements[i_elm].Node0 << " " << Elements[i_elm].Node1 << " " << Elements[i_elm].Node2 << std::endl;
			
		}


I can't access to the Node0,Node1,Node2 or size of element. it prints me nothing. how I can have a loop over the number of elements, get the Node0,Node1,Node2 and Elmsize?
Last edited on
Ok, you have shown that:
1
2
3
4
5
6
7
8
9
class Elements {
  // some private members
public:
  Element( VecIdx_t* , VecNode_t& );
  void set_vertices( VecIdx_t* );
};

// and
std::vector<Element> Elements;


These statements do absolutely nothing:
1
2
3
			Elements[i_elm].Node0;
			Elements[i_elm].Node1;
			Elements[i_elm].Node2;


Why do you do:
1
2
3
4
5
			Element elm(&connect, Nodes);
			Elements.push_back(elm);
			Elements[i_elm].set_vertices(&connect);

			std::cout << Elements[i_elm].Node0 ...

When you could:
1
2
3
4
5
			Element elm( &connect, Nodes );
			elm.set_vertices(&connect);
			Elements.push_back(elm);

			std::cout << Elements.back().Node0 ...

Or even:
1
2
3
4
			Element elm( &connect, Nodes );
			elm.set_vertices(&connect);
			std::cout << elm.Node0 ...
			Elements.push_back(elm);


(This assumes that you want to print data from the new Element.)

Why do you call Element::set_vertices() right after constructing Element? They seem to be partially redundant.

What are Node0,Node1,Node2? Why are they public?

Why is Assembler an Element?
Why is Assembler a Node?
Last edited on
Assembler is another class for assembling the global stiffness matrix in finite element method. I haven't finished writing of that class yet because I had some problem accessing data(class variable).It requires local stifness matrix and the local stifness matrix take the variable of class node and element.

Why nothing is happening here
1
2
3
Elements[i_elm].Node0;
			Elements[i_elm].Node1;
			Elements[i_elm].Node2;


when here I can do the same and get the results(Node class)

1
2
3
4
5
6
7
				VecNode_t Nodes;
				for (size_t i_node = 0; i_node < Nnodes; i_node++) {
					VecDbl_t coord = Global_Mesh.Coordinates_[i_node];
					Node node(&coord);
					Nodes.push_back(node);
std::cout<<Nodes[i_node].get_x()<<std::endl;
				}

Node0,Node1 and Node2 are class variables and elm is an object of class, why I can't access to the data in the way I showed?
I tried your way and still it's not printing anything.

and here is class Element,not Elements. Elements is the vector of Element objects.

1
2
3
4
5
6
class Element {
  // some private members
public:
  Element( VecIdx_t* , VecNode_t& );
  void set_vertices( VecIdx_t* );
};
Last edited on
Why nothing is happening here
Elements[i_elm].Node0;

What do you expect that to do?

Lets take similar case:
1
2
int x = 42;
x; // What does this statement do? 


I tried your way and still it's not printing anything.

Of course it does not print, because I did not nor could not do anything about that problem.

when here I can do the same ...
std::cout << Nodes[i_node].get_x();

That calls member function get_x() of a Node object.

Node0,Node1 and Node2 are class variables ...
1
2
3
4
5
6
class Element {
  // some private members
public:
  Element( VecIdx_t* , VecNode_t& );
  void set_vertices( VecIdx_t* );
};

If they are members of the Element, why didn't you show their declarations within the definition of Element?
That seems to be elemental for solving your issue.
Thanks for your answer. I showed them in Element header file.
I showed them in Element header file.

Which of your posts in this thread has that?
my bad, this thread doesn't have that. I am just confused with something. Imagine in a class we defined some variables. Then we have different member functions. These member functions are not single task, they do different things inside but in the middle they calculate some of those class variables.
For example N0,N1 and N2 are class variable and we have a member function
void dosomething(some arguments) and inside "dosomething" one of the outputs will be the N0. Now in order to get N0 should I first make an object of class, then call this member function like below?

Class object;
object.dosomething(some arguments);
object.N0;
is that the way to access member variable?
Last edited on
What you wrote is valid syntax. dosomething is a member function of the object of type 'Class', and N0 is a member variable of the object. You don't do anything with N0, however. I'm not sure if this answers your question. Perhaps reword it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class N {
    // ???  
};

class Class {
  public:
    void dosomething(int foo, int bar, int baz)
    {
        
    }
    
    N N0;
};

int main()
{
    Class object;
    object.dosomething(1, 2, 3);
    object.N0; // warning: statement has no effect [-Wunused-value]
}
We don't access member just to access member.
1
2
3
4
object.N0; // warning: statement has no effect [-Wunused-value]

foo = object.N0; // ok, read value of member and store it in 'foo'
object.N0 = bar; // ok, modify value of member 



Outsiders might not access any member variable directly, just call member functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Class {
private:
    N N0;
public:
    void dosomething(int foo, int bar, int baz)
    {
        // modify N0
    }
    
    double getresult()
    {
        // return something from N0
    }
};

int main()
{
    Class object;
    object.dosomething(1, 2, 3);
    double result = object.getresult();
}


Semi-trivial example:
1
2
3
4
5
6
int main()
{
    std::vector<int> data;
    data.push_back( 42 ); // modify
    std::cout << data.size(); // read
}
I do appreciate your explanations.it was helpful.
Last edited on
Topic archived. No new replies allowed.