incomplete type error - function object

A

Alan

I get a compilation error "parameter 2 of `Clusters
sorted(Clusters, func_obj)' has incomplete type `func_obj' " when
attempting to compile the following code:

some_clusters =
sorted(some_clusters,less_Eccentricity());

I`m trying to pass a function object (less_Eccentricity) into a
higher level function that uses it in the call to sort.

What is wrong with the use of the function object in the call
here? Also, below you will find some other code that compiled fine.
Am I missing something?

Thanks, Alan

Does Not Compile:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <math.h>
#include <stdexcept>


using namespace std;

struct measurement
{
string character;
string filename;
int Num_Regions;
int Euler_Number;
double Eccentricity;
};

typedef vector <measurement> Measurements;

struct cluster
{
double max;
double min;
Measurements measurements;
};

typedef vector <cluster> Clusters;

class less_Eccentricity : binary_function<measurement, measurement,
bool>
{
public:
result_type operator() (const measurement& a, const
measurement& b)
{
return a.Eccentricity < b.Eccentricity;
}
};


Clusters sorted(Clusters cluster_vector, class func_obj);

int main(int argc, char *argv[])
{

measurement data;
Measurements measurements;
cluster a_cluster;
Clusters some_clusters;

some_clusters = sorted(some_clusters,less_Eccentricity());

return EXIT_SUCCESS;
}

template <class T1>
Clusters sorted(Clusters cluster_vector, T1 func_obj)
{
for (int i = 0; i < cluster_vector.size(); i++)
{
for (int j = i + 1; j < cluster_vector.size(); j++)
if (cluster_vector.min > cluster_vector[j].min)
{
// swap structures
swap(cluster_vector,cluster_vector[j]);
}
else if (cluster_vector.min == cluster_vector[j].min)
if (cluster_vector.max < cluster_vector[j].max)
{
swap(cluster_vector,cluster_vector[j]);
}

sort(cluster_vector.measurements.begin(),cluster_vector.measurements.end(),func_obj);
}
return cluster_vector;
}


Compiles Fine:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>


using namespace std;

class measurement
{
public:
double metric1, metric2, metric3;
};

typedef vector<measurement> cluster;
typedef vector<cluster> Clusters;

class less_metric1 : binary_function<measurement, measurement, bool>
{
public:
result_type operator()(const measurement& a, const measurement& b)
{
return a.metric1 < b.metric1;
}
};

class less_metric2 : binary_function<measurement, measurement, bool>
{
public:
result_type operator()(const measurement& a, const measurement& b)
{
return a.metric2 < b.metric2;
}
};

class less_metric3 : binary_function<measurement, measurement, bool>
{
public:
result_type operator()(const measurement& a, const measurement& b)
{
return a.metric3 < b.metric3;
}
};

template <class T>
Clusters sorted_Clusters (Clusters Clusters_list, T func_obj)
{
for (int i = 0; i < Clusters_list.size(); i++)
{
for (int j = i + 1; j < Clusters_list.size(); j++)
{
// do some stuff
}

sort(Clusters_list.begin(),Clusters_list.end(),func_obj);
}
return Clusters_list;
}

template <class T>
void some_function (T in)
{
}

int main()
{
// Create objects
measurement data1, data2, data3, data4;
data1.metric1 = 1; data1.metric2 = 2; data1.metric3 = 3;
data2.metric1 = 2; data2.metric2 = 3; data2.metric3 = 2;
data3.metric1 = 3; data3.metric2 = 1; data3.metric3 = 1;
data4.metric1 = 4; data4.metric2 = 4; data4.metric3 = 4;
// put them into a vector of 2 clusters
cluster a_cluster;
Clusters some_clusters;
a_cluster.push_back(data1);
a_cluster.push_back(data2);
some_clusters.push_back(a_cluster);
a_cluster.clear();
// some_clusters.clear();
a_cluster.push_back(data3);
some_clusters.push_back(a_cluster);
a_cluster.push_back(data4);
some_clusters.push_back(a_cluster);
a_cluster.clear();

// Sort cluster data for each cluster
some_clusters = sorted_Clusters(some_clusters,less_metric1());

// Print out results
for (int i = 0; i < some_clusters.size(); i++)
{
cout << "Cluster " << i << "\n";
for (int j = 0; j < some_clusters.size(); j++)
cout << " " << (some_clusters)[j].metric1 <<
"\n";
}

system("PAUSE");
}
 
V

Victor Bazarov

Alan said:
I get a compilation error "parameter 2 of `Clusters
sorted(Clusters, func_obj)' has incomplete type `func_obj' " when
attempting to compile the following code:

some_clusters =
sorted(some_clusters,less_Eccentricity());

I`m trying to pass a function object (less_Eccentricity) into a
higher level function that uses it in the call to sort.

What is wrong with the use of the function object in the call
here? Also, below you will find some other code that compiled fine.
Am I missing something?

Thanks, Alan

Does Not Compile:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <math.h>
#include <stdexcept>


using namespace std;

struct measurement
{
string character;
string filename;
int Num_Regions;
int Euler_Number;
double Eccentricity;
};

typedef vector <measurement> Measurements;

struct cluster
{
double max;
double min;
Measurements measurements;
};

typedef vector <cluster> Clusters;

class less_Eccentricity : binary_function<measurement, measurement,
bool>
{
public:
result_type operator() (const measurement& a, const
measurement& b)
{
return a.Eccentricity < b.Eccentricity;
}
};


Clusters sorted(Clusters cluster_vector, class func_obj);

Since below [main] you define it as a template, you might want to
consider declaring it here as a templaet as well. Otherwise it's
a declaration of a function that takes some UNKNOWN class 'func_obj'
as its second argument. The compiler cannot figure out what the
heck 'func_obj' is. Ty

template<class F>
Clusters sorted(Clusters, F);

(also read about passing objects by a reference [to const]).
int main(int argc, char *argv[])
{

measurement data;
Measurements measurements;
cluster a_cluster;
Clusters some_clusters;

some_clusters = sorted(some_clusters,less_Eccentricity());

return EXIT_SUCCESS;
}

template <class T1>
Clusters sorted(Clusters cluster_vector, T1 func_obj)
{
for (int i = 0; i < cluster_vector.size(); i++)
{
for (int j = i + 1; j < cluster_vector.size(); j++)
if (cluster_vector.min > cluster_vector[j].min)
{
// swap structures
swap(cluster_vector,cluster_vector[j]);
}
else if (cluster_vector.min == cluster_vector[j].min)
if (cluster_vector.max < cluster_vector[j].max)
{
swap(cluster_vector,cluster_vector[j]);
}

sort(cluster_vector.measurements.begin(),cluster_vector.measurements.end(),func_obj);
}
return cluster_vector;
}


Compiles Fine:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>


using namespace std;

class measurement
{
public:
double metric1, metric2, metric3;
};

typedef vector<measurement> cluster;
typedef vector<cluster> Clusters;

class less_metric1 : binary_function<measurement, measurement, bool>
{
public:
result_type operator()(const measurement& a, const measurement& b)
{
return a.metric1 < b.metric1;
}
};

class less_metric2 : binary_function<measurement, measurement, bool>
{
public:
result_type operator()(const measurement& a, const measurement& b)
{
return a.metric2 < b.metric2;
}
};

class less_metric3 : binary_function<measurement, measurement, bool>
{
public:
result_type operator()(const measurement& a, const measurement& b)
{
return a.metric3 < b.metric3;
}
};

template <class T>
Clusters sorted_Clusters (Clusters Clusters_list, T func_obj)
{
for (int i = 0; i < Clusters_list.size(); i++)
{
for (int j = i + 1; j < Clusters_list.size(); j++)
{
// do some stuff
}

sort(Clusters_list.begin(),Clusters_list.end(),func_obj);
}
return Clusters_list;
}

template <class T>
void some_function (T in)
{
}

int main()
{
// Create objects
measurement data1, data2, data3, data4;
data1.metric1 = 1; data1.metric2 = 2; data1.metric3 = 3;
data2.metric1 = 2; data2.metric2 = 3; data2.metric3 = 2;
data3.metric1 = 3; data3.metric2 = 1; data3.metric3 = 1;
data4.metric1 = 4; data4.metric2 = 4; data4.metric3 = 4;
// put them into a vector of 2 clusters
cluster a_cluster;
Clusters some_clusters;
a_cluster.push_back(data1);
a_cluster.push_back(data2);
some_clusters.push_back(a_cluster);
a_cluster.clear();
// some_clusters.clear();
a_cluster.push_back(data3);
some_clusters.push_back(a_cluster);
a_cluster.push_back(data4);
some_clusters.push_back(a_cluster);
a_cluster.clear();

// Sort cluster data for each cluster
some_clusters = sorted_Clusters(some_clusters,less_metric1());

// Print out results
for (int i = 0; i < some_clusters.size(); i++)
{
cout << "Cluster " << i << "\n";
for (int j = 0; j < some_clusters.size(); j++)
cout << " " << (some_clusters)[j].metric1 <<
"\n";
}

system("PAUSE");
}
 
A

Alan

I just discovered this. What I don`t understand is I have used a
nearly identical (to the original) function that compiled fine.
Anyway, thanks.

Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top