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.

>>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();

Edited 6 Years Ago by Ancient Dragon: n/a

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.

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.

Edited 6 Years Ago by Fbody: n/a

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;
}

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. :)

This article has been dead for over six months. Start a new discussion instead.