Reputation: 977
i am using different classes and also objects relavent to those different classes. Now, i want to write a seperate function which use to joint two vector to another vector. where should i write these type of seperate functions. Can i use a new header file for that? actually, i made a new header file (ConnectedSegment.h) and put my function under that. but, i got this error.
43 D:myex\ConnectedSegment.h non-member function `std::vector<int>& NeighboursToGivenValue(std::map<int, std::vector<int> >&, int, int)' cannot have `const' method qualifier
D:\myex\Detects\Makefile.win [Build Error] [Detects.o] Error 1
Here is my code of the function:
vector<int> & NeighboursToGivenValue(map<int, vector<int> > &adjacency,
int s1, int s2) const
{
vector<int>::const_iterator any;
vector<int>::iterator is_any;
vector<int> *neighbours2both = new vector<int>;
int i;
neighbours2both->reserve(adjacency[s1].size() + adjacency[s2].size());
neighbours2both->insert(neighbours2both->end(), adjacency[s1].begin(),adjacency[s1].end ());
vector<int>& neighbours2s2=adjacency[s2];
for (any=neighbours2s2.begin(); any!=neighbours2s2.end(); any++){
is_any = find (neighbours2both->begin(), neighbours2both->end(), *any);
if(is_any == neighbours2both->end()){
if(s1 != *any) neighbours2both->push_back(*any);
}
}
for (i=0; i<neighbours2both->size(); i++){
neighbours2both->erase(neighbours2both->begin() + i);
}
return(*neighbours2both);
}
In here, I got the values for the adjacency()
by using another class called MyPoint
.
So I used myxyz.adjacency()
to accomadate the values for this adjacency. Now I dont want to call the same class MyPoint
for calling function NeighboursToGivenValue
. So,
could you tell me where I should write this function. Alternatively, if I write this function inside the MyPoint
class, how can I call this function without an object of that class.
Upvotes: 1
Views: 232
Reputation: 33645
These free functions can be in the header (will need to be declared inline
as Pedro highlights in his comment) - or they can be declared in the header and defined in an implementation file - for example,
#header
vector<int>& join_int_vector(vector<int> const& lhs, vector<int> const& rhs);
# cpp
vector<int>& join_int_vector(vector<int> const& lhs, vector<int> const& rhs)
{
// do stuff
}
The problem you face is that you can only have const member functions - i.e. free functions (and static functions) cannot be const
- it doesn't mean anything.
btw. don't construct a vector dynamically and return a reference to it, i.e:
vector<int> *neighbours2both = new vector<int>;
:
return (*neighbours2both);
The callee has no awareness that this object needs to be manually cleaned up - in this case, return by value.
vector<int> join_int_vector(vector<int> const& lhs, vector<int> const& rhs)
{
vector<int> neighbours2both;
:
return neighbours2both;
}
EDIT: for functions defined in the header itself, something like this:
inline vector<int> join_int_vector(vector<int> const& lhs, vector<int> const& rhs)
{
vector<int> neighbours2both;
:
return neighbours2both;
}
note the use of the keyword inline
- this prevents multiple definition errors if this header is included in multiple translation units.
Upvotes: 1