call derived member functions

Pages: 12
Maybe something like this:
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
struct MyBase
{	//	MyBase's member variables
	int		a1;
	int		b1;
	int		c1;
public:
	//	MyBase's constructor
	MyBase(int a, int b, int c)
	{	//	Note:  The arguments are on the right
		//	what they're being assigned to is on the left.
		a1 = a;
		b1 = b;
		c1 = c;
	}
};

struct MyDer : public MyBase
{	//	MyDer's member variables
        //      Currently unused
	int		a2;
	int		b2;
	int		c2;
public:
	//	MyDer's constructor
	//	MyDer's arguments are passed to MyBase's constructor
	MyDer(int a, int b, int c) : MyBase(a, b, c)
	{}
};

Last edited on
So finally a clue: you want to use IPOPT

https://coin-or.github.io/Ipopt/

The trouble is that you seem to know very little about C++, you have provided very little detail, not much of the information you have provided made any sense, anyone might think you are trolling us. There are people who post inept code and are very vague, in order to generate traffic on this site.

If that is not the case, you need to be much better at providing decent information, otherwise you may risk being ignored.

Which IPOPT software are you using?
What is the specific math function you want to use?
Can you use another language which you may be more familiar with: IPOPT seems to have a variety of interfaces.
@TheIdeasMan
Thank you for your comment
IPOPT is an open source software , and everyone can use it, yes there are interface to C and fortran 77 but I want to use C++ and its version the most popular is written on C++ so it is better to use that languages instead to use old language like F77. I use IPOPT version 3.5.4. I want to define my problem in derived classes (i.e., parameters, functions that define objection function, gradient of objectif function, hessian of the Lagrangien...). The file.cpp define predefined methods of IPOPT library, so the idea is to use derived classes to define the whole problem and then call these details on file.cpp to include them on predefined methods. Please let me learn and that would be realized with your help and contribution.
Thank you very much
@AbstractionAnon
Why does the below organization does not give me the value of member_1=value1 ,.......

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
//in file.hpp
class Base 
{
public:
  
  

virtual void fun(double member1, double member2, double member3, ....);


private:

  double member_1;
  double member_2;
  double member_3;
  ....
};


class Derived : public Base
{ 
public:
 

  
void fun(double member1, double member2, double member3, ....)

{

 member1 =value1;
 member2 = =value2;
 member3 = =value3;
}

};

//in file.cpp

void Base::fun(double member1, double member2, double member3, ....)
{

Base* b = new Base;
Derived* d = new Derived;
b = d;
b->fun(member1, member2, member3, ....)
    
 member_1 = b->member1,
member_2 = b->member2;
member_3 = b->member3;
delete b

}

Last edited on
https://rubberduckdebugging.com/

> does not give me the value of member_1=value1
¿so does that crap actually compile? ¿and it gives your erroneous output?
post your actual code
if your actual code is a mess of of 5000 lines, then throw it to the garbage and start anew.

no idea what `value1' is, there you are assigning to the parameter passed to the function
if you don't give a damn about the parameter then don't ask for it.


> I want to use IPOPT to my own problem
don't know what that is

> The purpose is to creat derived classes to define my probem, its parameters, grad of function and constraint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//pseudocode
class Base:
  problem()
  grad_function()
  constraints()

solver(Base b):
  //magic
  return 42


class Derived1 inherit Base:
  problem() // provide a definition to these functions
  grad_function()
  constraints()
  speed, mass //some parameters


a = new Derived
a.speed = 0.5c
a.mass = 1g
solver(a)
¿something like that?
¿what's the problem? ¿setting speed and mass?
Last edited on
Lines 31-33: I've told you twice, you don't assign values to arguments (unless they're passed by reference). These lines do nothing.

Line 45 is a memory leak. You've just lost the b you created.

You're clearly trying to do things in C++ that are beyond you.
I'm done, especially when you don't fix the errors that have been pointed out to you.
Last edited on
@AbstractionAnon
I do not get the values of member_1 , member_2 ....
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//In file.hpp
class Base : public Source
{
public:
  
 Base();
  
 virtual ~Base();

virtual void fun(double member1, double member2, double member3, ....);


private:

  double member_1;
  double member_2;
  double member_3;
  ....
};


class Derived : public Base
{ 
public:

Derived()
{}
  
virtual ~Derived()
{}

  

double aa=1.2;
double ab=2.2;
double ac=4.5;



void fun(double member1, double member2, double member3, ....)

{

 member1 = aa;
 member2 = ab;
 member3 = ac;
}

};

//In file.cpp

void Base::fun(double member1, double member2, double member3, ....)
{
 
Base* bb = new Base;
Derived* dd = new Derived;
// when I make this:
bb->fun(double member1, double member2, double member3, ....)
member_1 = bb->member1;


//the compiler says that  'class Base' has no member named 'member1' did you mean member_1

//and when I make this:

dd->fun(double member1, double member2, double member3, ....)
member_1 = dd->member1;

//the compiler says 'class Derived' has no member named 'member1' did you mean member_1




}



What was worng please
Thank you,
Last edited on
First of all, that is not an example that could be compiled. Yes, you have errors, but you have also bits that distract from those errors.

For example, the ..... That is not valid C++ and you clearly do not even intent it to be valid.

Then, the Base is actually a class derived from class Source. You don't show code of that class, so we could never succeed, even if your code were correct.

Last, there is no main() function that would use these types.


Lets cut some out:
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
30
31
32
33
34
35
36
37
38
39
40
41
class Base
{
public:
  Base() {}
  virtual ~Base() {}
  virtual void fun( double member1 );
private:
  double member_1;
};

class Derived : public Base
{ 
public:
  double aa = 1.2;
  Derived() {}
  virtual ~Derived() {}
  void fun( double member1 );
};


void Base::fun( double member1 )
{
  Base* bb = new Base;
  Derived* dd = new Derived;
  bb->fun( double member1 ) // error: expected primary-expression before 'double'
  member_1 = bb->member1;

  dd->fun( double member1 ) // error: expected primary-expression before 'double'
  member_1 = dd->member1;
}


void Derived::fun( double member1 )
{
 member1 = aa;
}


int main()
{
}

The bb->fun( double member1 ) makes no sense.
It has no semicolon at end. The statement that you have here is actually:
bb->fun( double member1 ) member_1 = bb->member1 ;
Which makes even less sense.

If you want to call a function, then you must supply a value, not a declaration as argument (and must have semicolon at right place):
bb->fun( 3.14 ) ;

In the simplified example here, the Base object has exactly one data member: double member_1
The Derived object has two members: the double aa and also the double member_1, because it inherits Base.

The Derived::fun() has parameter double member1 but that variable is local to the function. Effectively, the Derived::fun() does absolutely nothing.

You can call the Base::fun() only if you already have a Base object. Why do you create a second Base and one Derived objects within the function (and never deallocate those dynamically allocated objects)?

If your Base::fun() creates a Base object and calls Base::fun(), the second call will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), which will create a Base base and call Base::fun(), ... until computer runs out of memory.
@keskiverto
Still have the same error , error: ‘class Base’ has no member named ‘member1; did you mean ‘member_1. I use the following:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//In file.hpp
class Base
{
public:
  Base();
  virtual ~Base();
  virtual void fun( double member1, double member 2, double member 3);
private:
  double member_1;
double member_2;
double member_3;
};

class Derived : public Base
{ 
public:
  double aa = 1.2;
 double bb = 2.2;
 double cc = 3.2;

 Derived() 
{}
 virtual ~Derived() 
{}
  void fun( double member1 , double member 2, double member 3);
};

//In file.cpp

 Base* bb = new Base;
  Derived* dd = new Derived;
void Base::fun( double member1, double member 2, double member 3 )
{
 
  member_1 = bb->member1;

}


void Derived::fun( double member1, double member 2, double member 3)
{
 member1 = dd->aa;
}


’?
Last edited on
@gadi01

I think your last post just proves that you are wasting everyone's time. If traffic generation was your goal, then congratulations you received 28 replies. FWIW i reported you.
@TheIdeasMan
what do you say???? this is not true. why you accuse me with thing that is absolutely false. I ask Sir @keskiverto why the code does not give me the values. I try to learn c++ and I say may times in my comments about my purpose. So please do not accuse people when you have no prove.
I ask the members of the forum to help please to achieve progress in my work.
Thank you
I agree with TheIdeasMan. You're a troll. YOU DO NOT LISTEN. kbw, ne555, keskiverto and I have all pointed out things wrong with your program and you do not correct them.
do not insult !!!!! why do not respect beginners to lean. I still learn C++ as everyone leran something else. if someone do not like to give help to persons so it is better to keep respect to them.
gadi01, I can see why the others are getting frustrated with you.

Don't take this the wrong way but if I half understand the last load of code that you posted, I would say you have a fundamental misunderstanding of inheritance and suggest you address that first. Then, if you are truly not a troll and are just a beginner out of your depth, I would say reread the replies that you have been given.
I did what I understand and I want to know why the code is still keeping give me the same error . I ask where is the wrong thing that can correct for that error goes away
You wrote:
1
2
3
4
void Base::fun( double member1, double member 2, double member 3 )
{
  member_1 = bb->member1;
}

This function attempts to assign a value to member_1. That is ok, because Base has member member_1.

The assigned value comes from bb->member1. What is that?
* The bb is not a member of Base
* The bb is not a parameter of the function
What is it then?

What is the purpose of the parameters ( double member1, double member 2, double member 3 )? You don't use them in the function.
The double member 2, double member 3 are a syntax error.

Here is a function that assigns value that it gets as parameter into its member:
1
2
3
4
void Base::fun( double sad )
{
  member_1 = sad;
}


Now we can write a program:
1
2
3
4
int main() {
  Base obj;
  obj.fun( 3.14 ); // call of member function assigns 3.14 to obj.member_1
}


If you ask why your code that you post repeatedly -- blatantly ignoring comments given to you -- is in compilers' opinion consistently nonsense ... the guess by others was already as good as any.
@keskiverto
Thank you
I said in my previous comments that my problem has many parameters and functions and I use the above example as learning to know how can I transfer member functions with their parameters from derived classes to Base_Class_file.cpp. That is, my routine does not contain one member1 . I define member_1 as private in Base class and I want to assign a value from derived class to it. That is, the purpose is to define const double members, variables, and functions, and then , one can access them from derived classes to Base_Class_file.cpp instead to main program
I define member_1 as private in Base class and I want to assign a value from derived class to it.


Which is what will never work because private: means members are private to that class, any other classes including derived and base one can't access private members of another class.

You should declare those members as protected: instead, which will allow you to assign them in derived class, but protect them to be used outside the class directly.

I suggest you to read one good C++ book from start to end, to learn these things in detail, and make sure you understand and practice described concepts, because it's wrong to learn this by the method of "trial and error".

The method of "trial and error" is wrong because you will stumble upon many obstacles that serve no purpose except wrong memorization of things that you learn, which in the future results in issues writing code.

For example:
"C++ Pimer 5th edition" or "Effective C++" are good books to begin with.
Later you can find some book that helps to write and understand template code, or books that help to understand the "modern" way of C++, ex. "Effective modern C++"

You can also find free PDF books online, although avoid books such as "C++ in 21 days" because it won't get you any good.
Last edited on
This page has a fully worked cpp example.

https://coin-or.github.io/Ipopt/INTERFACES.html

Contrast with all the nonsense we have had from the OP.
Topic archived. No new replies allowed.
Pages: 12