Hi guys :)

I'm stuck with same compiler messages since yesterday, and thought it might be helpful to aks for help :)

The prog I'm developing is relatively big, so I'll post just snippets in order to describe what I'm doing.

So, I have a class with a few subclasses. After parsing a file line, an object of the particular subclass is generated and appended to a vector:

class Circle:public GeomElem{};
. . .
vector<Circle*> CircleElems;
CircleElems.push_back(new Circle(Center_, Radius_, ID_));

In the next step I have defined a functor to set a predicate for a find_if(), so I can look for a certain circle by its ID in the vector:

struct FindIDCircle{
private:
	string SearchID_;
public:
	FindIDCircle(string ID_) : SearchID_(ID_){}
	bool operator()(Circle* findElem){
		return findElem->ID == SearchID_;
	}
};

After being ready with parsing the file lines and have generated all objects needed, I want to set a matrix to keep (e.g.) the center points of the circles. Therefore, I import the header of the file I fill the public circle vector and

int matrixSize(ParserDVG::NodeElems.size());

And I get the same 2 errors for a day now:

error C2440: '<function-style-cast>' : cannot convert from 'std::string' to 'ParseFile::FindIDCircle'
error C2228: left of '.size' must have class/struct/union

Does anyone have any idea? I'm close to desperation :(
Thanks in advance.

Dani AI

Generated

Two separate problems are visible in the thread. already caught the “function-style” declaration issue: writing something that looks like a type name with parentheses can be parsed as a function prototype (the classic “most vexing parse”), so the expression intended to query a container’s size can end up being a declaration instead. That’s one source of the “left of ‘.size’ must have class/struct/union” symptoms.

The C2440 / “cannot convert from 'std::string' to 'Parser::FindIDCircle'” combined with “Source or target has incomplete type” strongly suggests the functor type is incomplete or its constructor isn’t visible at the call site. Common causes seen in these threads are: the predicate struct is declared inside a namespace or class (e.g. Parser::FindIDCircle) but only forward-declared where find_if is used; or the constructor/operator() were defined outside the class with the wrong qualification, so the compiler never sees a usable constructor (which also explains the C2514 message reported earlier).

A safe, minimal corrective example for an out-of-class constructor (when the functor is nested) is:

Parser::FindIDCircle::FindIDCircle(const std::string& id)
  : SearchID_(id)
{ }

Notes and checklist based on the thread: prefer const std::string& for the constructor, make operator() const if possible, ensure the functor’s full definition is included before the find_if call (or make it inline in the header), and if Circle::ID is private use a public accessor or friend the predicate. To isolate the compiler’s complaint, instantiate the predicate in its own statement (e.g. Parser::FindIDCircle p(myID);) — that often yields a clearer error pointing to the missing definition or wrong qualification. Finally, the working example from demonstrates the pattern when all definitions are visible in the same translation unit, which is a useful reference when reorganizing headers/implementation.

Recommended Answers

All 12 Replies

>>int matrixSize(ParserDVG::NodeElems.size());

That looks like a function prototype. You can't put executable code such as .size() there.

It's not, I only have the false sequence pasted. Meant as

int matrixSize(Parser::CircleElems.size());

CircleElems is the vector of pointers at the created Circle-objects.

Is this a variable declaration that uses explicit initialization?

If so, try an implicit initialization:

int matrixSize = Parser::CircleElems.size();

Doesn't work either. But actually the functor problem is more urgent, until I have more time to deal with the initialization I just moved the function in the same source file as the CircleElems vector was built.

Thanks for answering - and if anyone has an idea about the <function-style-cast> problem - any help is appreciated :)

>>int matrixSize(Parser::CircleElems.size());

Same problem I mentioned before -- that is a function prototype. It does not declare matrixSize as a POD int, but as a function.

Is CircleElems vector a non-static member of class Circle? If it is then it can only be accesses via an instance of the Circle class. For example

Circle c;
int matrixSize = c.CircleElements.size();

Oh .. sorry, I got you wrong - I thought you meant the Parser::CircleElems part seemed as a function.

Ok - but since the vector is defined as a public global it would keep its entries until the compiler has been through the whole code, even if I would generate a random instance only toaccess the vector size, right? I couldnot test it since I have this other error.

Noone has an idea about this functor thing?

Is this the error that you are having trouble with?

error C2440: '<function-style-cast>' : cannot convert from 'std::string' to 'ParseFile::FindIDCircle'

Since we don't have any reference line numbers, it's hard to say, but I suspect this line is the problem:

FindIDCircle(string ID_) : SearchID_(ID_){}

In this line, you have a string parameter for FindIDCircle() which you subsequently send to the Initializer for "SearchID". Is "SearchID" actually a class with a proper constructor that accepts a std::string as a parameter or is it a function with a different prototype? Please post it's full prototype and definition.

Hi there :)

No, SearchID is simply a string supposed to be passed when calling the constructor FindIDCircle of the predicate struct:

struct FindIDCircle{
private:	
   string SearchID_;
public:	
   FindIDCircle(string ID_) : SearchID_(ID_){}
   bool operator()(Circle* findElem){		
      return findElem->ID == SearchID_;
	}
   };

FindIDCircle is a constructor which initializes the privat string SearchID_ with the string ID_ that the constructor has been called with.

The error is dumped at the line the find_if is called:

string ID = FileLine.substr(18,30);
std::vector<Circle*>::iterator itCE = find_if(CircleElems.begin(), CircleElems.end(), FindIDCircle(ID));

The error message:

'<function-style-cast>' : cannot convert from 'std::string' to 'Parser::FindIDCircle'
1> Source or target has incomplete type

In a similar struct I set a constructor accepting a number of strings and comparing them with object attributes I get the error message

C2514: class has no constructors

. I don't know if this has the same reason - the structs are built the same way though. Maybe this gives you a hint :)

Thanks in advance :)

Hi,

I think you are not using it properly, It should work, i just copy paste your code and its working for me.

I hope it helps:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Circle{
private:

	string ID; // FindIDCircle have to declare as friend of Circle to access it, or

public:
	//string ID; //Make it public if you dont want friend FindIDCircle, or use getID

	Circle(string ID): ID(ID){}
	~Circle(){ cout << "I am dying " << ID << endl; };

	string getID(){ return ID;}
};

struct FindIDCircle{

private:
	string SearchID_;
public:
	FindIDCircle(string ID_) : SearchID_(ID_){}
	bool operator()(Circle* findElem){
		//return findElem->ID == SearchID_;
		return findElem->getID() == SearchID_;
	}

};


int main() {
	
	vector<Circle *> vec;
	Circle *circle;

	circle = new Circle("ID1");
	vec.push_back(circle);

	circle = new Circle("ID2");
	vec.push_back(circle);

	vec.push_back(new Circle("ID3"));
	vec.push_back(new Circle("ID4"));

	vector<Circle *>::iterator result;

	result = find_if(vec.begin(), vec.end(), FindIDCircle("ID4"));


	if(result != vec.end()){
		Circle *circle = *result;
		cout << circle->getID() << endl;
	}
	else cout << "Circle not found" << endl;

	return 0;
}

Oh, I see what's going on here. You're trying to create a "custom" comparison function / Predicate. I've never had good luck with that so I really can't help you any more. Hopefully, this example will help.

BTW I have deliberately put this code:

~Circle(){ cout << "I am dying " << ID << endl; };

You wont see any output of this for the above code snippet of mine.

Please use shared_ptr to avoid this kind of memory leak. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.