1
I am a beginner in programming and decides to venture into a project with some friends, in it I need the program descending which the order of the routine based on a factor, I am passing to a smaller chained list to facilitate the organization and have to deal with the whole class.
However I am finding problem to pass the method to function. Follow an example code:
#include <iostream>
#include <string>
using namespace std;
class Txt
{
string palavra;
int tam;
public:
void load(string txt) { palavra = txt; tam = txt.length(); }
void print() { cout << palavra << endl; };
int w_tam() { return tam; }
};
class Info2
{
public:
int tam;
void (*print)();
};
Info2 redirecionar(Txt txt)
{
Info2 info;
info.tam = txt.w_tam();
info.print = &(txt.print); // aqui acontece um erro
return info;
}
int main()
{
Txt A, B;
A.load("Amor");
B.load("Bolota");
Info2 AA, BB;
AA = redirecionar(A);
BB = redirecionar(B);
if (AA.tam > BB.tam)
{
AA.print();
}
else
{
BB.print();
}
}
I use VS2017, and error has the following description: '&' invalid operation in the associated member function expression. But I could not understand, I saw the same idea in another topical in C.
You need to decide whether to do it in C++ or C. This code mixes the two things.
– Maniero
The guy from
Txt::print
is notvoid (*)()
, and yesvoid (Txt::*)()
.– Mário Feroldi
The idea would be to do this in C++, as I do?
– Guilherme Stabach Salustiano